[英]How do i get the month names in between two dates using Java
我試圖找出從本月名稱01/01/2014
至12/28/2014
在“格式MMM
, YY
”。 但是我得到下面的輸出。
DEC, 14.
JAN, 14.
FEB, 14.
MAR, 14.
APR, 14.
MAY, 14.
JUN, 14.
JUL, 14.
AUG, 14.
SEP, 14.
OCT, 14.
NOV, 14.
我需要這樣的輸出
Jan, 14
.......
.......
Dec, 14
任何人都可以幫忙。
這是我的代碼。
String date1 = "01/01/2014";
String date2 = "12/28/2014";
final String OLD_FORMAT = "MM/dd/yyyy";
final String NEW_FORMAT = "MMM, YY";
SimpleDateFormat newFormat = new SimpleDateFormat(OLD_FORMAT);
Date fromDate = newFormat.parse(date1);
Date toDate = newFormat.parse(date2);
newFormat.applyPattern(NEW_FORMAT);
String newFromDate = newFormat.format(fromDate);
String newToDate = newFormat.format(toDate);
//System.out.println(newFromDate);
//System.out.println(newToDate);
DateFormat formater = new SimpleDateFormat(NEW_FORMAT);
Calendar beginCalendar = Calendar.getInstance();
Calendar finishCalendar = Calendar.getInstance();
try {
beginCalendar.setTime(formater.parse(newFromDate));
finishCalendar.setTime(formater.parse(newToDate));
} catch (ParseException e) {
e.printStackTrace();
}
String date;
while (beginCalendar.before(finishCalendar)) {
// add one month to date per loop
date = formater.format(beginCalendar.getTime()).toUpperCase();
System.out.println(date);
beginCalendar.add(Calendar.MONTH, 1);
}
提前致謝。
您可以嘗試:
String date1 = "01/01/2014";
String date2 = "12/28/2014";
final String OLD_FORMAT = "MM/dd/yyyy";
final String NEW_FORMAT = "MMM, YY";
SimpleDateFormat newFormat = new SimpleDateFormat(OLD_FORMAT);
DateFormat formatter = new SimpleDateFormat(NEW_FORMAT);
Date fromDate = newFormat.parse(date1);
Date toDate = newFormat.parse(date2);
Calendar beginCalendar = Calendar.getInstance();
Calendar finishCalendar = Calendar.getInstance();
beginCalendar.setTimeInMillis(fromDate.getTime());
finishCalendar.setTimeInMillis(toDate.getTime());
String date;
while (beginCalendar.before(finishCalendar)) {
// add one month to date per loop
date = formatter.format(beginCalendar.getTime());
System.out.println(date);
beginCalendar.add(Calendar.MONTH, 1);
}
Java 8和更高版本中內置的java.time框架取代了麻煩的舊java.util.Date/.Calendar類。 新課程的靈感取自於成功的Joda-Time框架,該框架旨在作為其繼任者,其概念相似但經過重新架構。 由JSR 310定義。 由ThreeTen-Extra項目擴展。 請參閱教程 。
新類別包括LocalDate
與日期,只值SANS時間的日和時區工作。 從那里開始,我們將使用YearMonth
對象,因為您對年份和月份很感興趣。
首先將兩個字符串解析為一對LocalDate
實例。
String inputStart = "01/01/2014";
String inputStop = "12/28/2014";
DateTimeFormatter formatterInput = DateTimeFormatter.ofPattern ( "MM/dd/yyyy" );
LocalDate start = LocalDate.parse ( inputStart , formatterInput );
LocalDate stop = LocalDate.parse ( inputStop , formatterInput );
我們將生成YearMonth
對象的String表示形式。 YearMonth::toString
方法的默認格式是標准ISO 8601格式YYYY-MM。 我強烈建議在可行的情況下使用這些格式。 但是您的Question指定了不同的格式,因此我們必須定義一個自定義格式器模式。
注意顯式的Locale
對象,例如Locale.ENGLISH
或Locale.CANADA_FRENCH
。 如果省略,則會隱式應用JVM的當前默認語言環境來本地化月份名稱。 默認值可以在運行時隨時更改,因此我建議始終指定所需的/預期的語言環境。
DateTimeFormatter formatter = DateTimeFormatter.ofPattern ( "MMM yyyy" , Locale.ENGLISH ); // Or Locale.CANADA_FRENCH etc.
我們不使用System.out.println
,而是將每個月的String輸出收集到List
。
List<String> outputs = new ArrayList<> ();
為我們的起始LocalDate實例化YearMonth
。
YearMonth yearMonth = YearMonth.from ( start );
從該年yearMonth開始,循環一個YearMonth對象序列,生成該年月值的String表示形式。 繼續循環直到我們LocalDate
結束LocalDate
的年-月為止。 在每個循環中,我們通過調用plusMonths
遞增。
// Repeat while the current year-month is *not* after (is before or is equal) the ending year-month.
while ( ! yearMonth.isAfter ( YearMonth.from ( stop ) ) ) {
//String output = yearMonth.toString (); // ISO 8601 format.
String output = yearMonth.format ( formatter );
outputs.add ( output );
// Prepare for next loop.
yearMonth = yearMonth.plusMonths ( 1 );
}
轉儲到控制台。
System.out.println ( "Months from start: " + start + " to stop: " + stop + " is: " + outputs );
從開始日期:2014年1月1日到結束日期:2014年12月28日是:[2014年1月,2014年2月,2014年3月,2014年4月,2014年5月,2014年6月,2014年7月,2014年8月,2014年9月,2014年10月, 2014年11月,2014年12月]
我強烈建議不要使用兩位數的年份作為輸出。 我發現這么短的年份會導致業務混亂。 但是,如果您堅持要求,請將模式從yyyy
調整為yy
。
提示:由於YearMonth
已內置在Java 8和更高版本中,因此,如果您要對YearMonth
-month值進行更多的操作,而不是像在Question中看到的那樣轉儲月份名稱,則建議在整個代碼中使用此類的對象。
可以使此代碼更健壯。 例如,檢查結束的LocalDate確實是起始的isEqual
或isAfter
(不是isBefore
)。 你也許能夠把持的結果來優化位YearMonth.from ( stop )
在一個變量,而不是在循環反復調用,但我不會理會,除非你經常用這個代碼-和JVM很可能是最優化自動為您服務。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.