java 如何获取两个日期之间的月份和年份列表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29479479/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get list of months & years between two dates
提问by 99maas
I need your help in getting the list of months and the years in String between two dates. The user will enter two dates in the String format of:
我需要你的帮助来获取两个日期之间的字符串中的月份和年份列表。用户将以字符串格式输入两个日期:
String date1 ="JAN-2015";
String date2 ="APR-2015";
So the result should be:
所以结果应该是:
- Jan-2015
- FEB-2015
- MAR-2015
- 2015 年 1 月
- 2015 年 2 月
- 2015 年 3 月
I tried using the following code but it gave me wrong results:
我尝试使用以下代码,但它给了我错误的结果:
List<Date> dates = new ArrayList<Date>();
String str_date ="JAN-2015";
String end_date ="APR-2015";
DateFormat formatter ;
formatter = new SimpleDateFormat("MMM-yyyy");
Date startDate = formatter.parse(str_date);
Date endDate = formatter.parse(end_date);
long endTime =endDate.getTime() ;
long curTime = startDate.getTime();
while (curTime <= endTime) {
dates.add(new Date(curTime));
curTime ++;
}
for(int i=0;i<dates.size();i++){
Date lDate =(Date)dates.get(i);
String ds = formatter.format(lDate);
System.out.println(ds);
}
回答by Knomo Seikei
Using the less code possible and basic java libraries and getting the result you asked for. So you can modify the date1 and date2 variables.
使用尽可能少的代码和基本的 Java 库并获得您要求的结果。所以你可以修改 date1 和 date2 变量。
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
public class Main {
public static void main(String[] args) {
String date1 = "JAN-2015";
String date2 = "APR-2015";
DateFormat formater = new SimpleDateFormat("MMM-yyyy");
Calendar beginCalendar = Calendar.getInstance();
Calendar finishCalendar = Calendar.getInstance();
try {
beginCalendar.setTime(formater.parse(date1));
finishCalendar.setTime(formater.parse(date2));
} catch (ParseException e) {
e.printStackTrace();
}
while (beginCalendar.before(finishCalendar)) {
// add one month to date per loop
String date = formater.format(beginCalendar.getTime()).toUpperCase();
System.out.println(date);
beginCalendar.add(Calendar.MONTH, 1);
}
}
}
回答by Sergey Pauk
In case your Java version is < 8 you could use Calendar as follows:
如果您的 Java 版本 < 8,您可以按如下方式使用 Calendar:
private final static DateFormat formatter = new SimpleDateFormat("MMM-yyyy", Locale.ENGLISH);
public static void main(String[] args) throws ParseException {
Calendar startDate = stringToCalendar("Jan-2015");
Calendar endDate = stringToCalendar("Apr-2015");
while (startDate.before(endDate)) {
System.out.println(formatter.format(startDate.getTime()));
startDate.add(Calendar.MONTH, 1);
}
}
private static Calendar stringToCalendar(String string) throws ParseException {
Date date = formatter.parse(string);
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
return calendar;
}
If you have a luxury of Java 8 then the code becomes more simple:
如果你有 Java 8 的奢侈品,那么代码会变得更简单:
public static void main(String[] args) {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MMM-yyyy", Locale.ENGLISH);
YearMonth startDate = YearMonth.parse("Jan-2015", formatter);
YearMonth endDate = YearMonth.parse("Apr-2015", formatter);
while(startDate.isBefore(endDate)) {
System.out.println(startDate.format(formatter));
startDate = startDate.plusMonths(1);
}
}