2016-09-21 274 views
-2

我需要幫助獲取當年開始和結束日期,去年和明年如何在Android中獲取開始日期和結束日期?

以下是我的代碼:這段代碼在月份可以正常工作,我可以修改它嗎?

注意:此代碼僅用於舉例。

protected void getDataByMonths(int currentDayOfMonth) { 

     Calendar calendar = Calendar.getInstance(); 

     int year = calendar.get(Calendar.YEAR); 

     int month; 

     if (currentDayOfMonth >= 2) { 

      month = calendar.get(Calendar.MONTH) + 1; 
     } else { 

      month = calendar.get(Calendar.MONTH) - currentDayOfMonth; 
     } 
     int day = 1; 

     calendar.set(year, month, day); 

     DateFormat df = new SimpleDateFormat("MM/dd/yyyy"); 

     int numOfDaysInMonth = calendar.getActualMaximum(Calendar.DAY_OF_MONTH); 

     String firstday = String.valueOf(df.format(calendar.getTime())); 

     calendar.add(Calendar.DAY_OF_MONTH, numOfDaysInMonth - 1); 

     String lastday = String.valueOf(df.format(calendar.getTime())); 


     String result = getButtonName(button) + " From :" + getDateInMonthFormat(firstday) + " " + "To :" + getDateInMonthFormat(lastday); 

     finalcontacts = mySqliteDBhelper.getContactsBetweenRange(button, getDateInMilliseconds(firstday), getDateInMilliseconds(lastday)); 

     finalstatus.setText(result); 
    } 

回答

2

檢查:

public static String GetYearSlot(int option,String inputDate) 
    { 
     SimpleDateFormat sdf = new SimpleDateFormat("dd MMM yyyy",java.util.Locale.getDefault()); 
     Date myDate = null; 
     try 
     { 
      myDate = sdf.parse(inputDate); 
     } 
     catch(Exception ex) 
     { 
      ex.printStackTrace(); 
     } 
     Calendar calendar = Calendar.getInstance(); 
     calendar.setTime(myDate); 

     calendar.add(Calendar.YEAR, option); 
     calendar.set(Calendar.DAY_OF_YEAR, 1); 
     Date YearFirstDay = calendar.getTime(); 
     calendar.set(Calendar.MONTH, 11); 
     calendar.set(Calendar.DAY_OF_MONTH, 31); 
     Date YearLastDay = calendar.getTime(); 

     return sdf.format(YearFirstDay)+"-"+sdf.format(YearLastDay); 
    } 

如何使用:

GetYearSlot(1,FROM日期):它可以讓你從你通過的日期在明年(輸入1)

GetYearSlot(0,fromDate):從您通過的日期開始計算當年(輸入0)

GetYearS很多(-1,FROM日期):它給你上一年度之日起您傳遞(輸入-1)

1

假設你不能使用Java 8,這裏是如何能夠做到:

SimpleDateFormat df = new SimpleDateFormat("MM/dd/yyyy"); 

// Create first day of year 
Calendar firstDayOfCurrentYear = Calendar.getInstance(); 
firstDayOfCurrentYear.set(Calendar.DATE, 1); 
firstDayOfCurrentYear.set(Calendar.MONTH, 0); 
System.out.println(df.format(firstDayOfCurrentYear.getTime())); 

// Create last day of year 
Calendar lastDayOfCurrentYear = Calendar.getInstance(); 
lastDayOfCurrentYear.set(Calendar.DATE, 31); 
lastDayOfCurrentYear.set(Calendar.MONTH, 11); 
System.out.println(df.format(lastDayOfCurrentYear.getTime())); 

// Create first day of next year 
Calendar firstDayOfNextYear = Calendar.getInstance(); 
firstDayOfNextYear.add(Calendar.YEAR, 1); 
firstDayOfNextYear.set(Calendar.DATE, 1); 
firstDayOfNextYear.set(Calendar.MONTH, 0); 
System.out.println(df.format(firstDayOfNextYear.getTime())); 

// Create last day of next year 
Calendar lastDayOfNextYear = Calendar.getInstance(); 
lastDayOfNextYear.add(Calendar.YEAR, 1); 
lastDayOfNextYear.set(Calendar.DATE, 31); 
lastDayOfNextYear.set(Calendar.MONTH, 11); 
System.out.println(df.format(lastDayOfNextYear.getTime())); 

輸出:

01/01/2016 
12/31/2016 
01/01/2017 
12/31/2017 
0

java.time

您現在使用由java.time類取代了麻煩的舊的遺留日期時間類。

首先獲取當前日期。

LocalDate類代表沒有時間和不帶時區的僅有日期的值。

時區對確定日期至關重要。對於任何特定的時刻,日期因地區而異。例如,Paris France午夜後幾分鐘是新的一天,而在Montréal Québec仍然是「昨天」。

ZoneId z = ZoneId.of(「America/Montreal」); 
LocalDate today = LocalDate.now(z); 

使用Year來表示整個一年的對象。

Year thisYear = Year.from(today); 
Year nextYear = thisYear.plusYears(1); 
Year lastYear = thisYear.minusYears(1); 

通常在日期時間工作中,我們使用Half-Open方法表示一段時間。在這種方法中,開頭是,包括,而結尾是,獨家。因此,一年將從一月一號開始,並延續到下一年的一月一號,但不包括在內。

如果在Java 8上,您可以包含ThreeTen-Extra項目及其Interval類來表示時間跨度。

否則自己動手。

LocalDate thisYearStart = thisYear.atDay(1); 
LocalDate lastYearStart = lastYear.atDay(1); 
LocalDate nextYearStart = nextYear.atDay(1); 

如果您確實需要一年的最後一天,您可以從下一年的第一天減去一天。更容易的是使用TemporalAdjusters類中定義的TemporalAdjuster

LocalDate thisYearFirstDay = today.with(TemporalAdjusters.firstDayOfYear()); 
LocalDate thisYearLastDay = today.with(TemporalAdjusters.lastDayOfYear()); 

LocalDate nextYearFirstDay = thisYearLastDay.plusDays(1); 
LocalDate nextYearLastDay = nextYearFirstDay.with(TemporalAdjusters.lastDayOfYear()); 

LocalDate lastYearLastDay = thisYearFirstDay.minusDays(1); 
LocalDate lastYearFirstDay = lastYearLastDay.with(TemporalAdjusters.firstDayOfYear()); 

關於java.time

java.time框架是建立在Java 8和更高版本。這些類代替了令人討厭的舊日期時間類,例如java.util.Date,.Calendar,& java.text.SimpleDateFormat

Joda-Time項目現在位於maintenance mode,建議遷移到java.time。請參閱Oracle Tutorial。並搜索堆棧溢出了很多例子和解釋。

大部分的java.time功能後移植到Java 6 和ThreeTenABP還適於Android(見How to use…)。

ThreeTen-Extra項目擴展java.time與其他類。這個項目是未來可能增加java.time的一個試驗場。您可以在這裏找到一些有用的類,如Interval,YearWeek,YearQuartermore

相關問題