我需要两个给定日期之间的所有月份/年份名称。我只需要在java上输出。
示例:
Input Date:
date 1- 20/12/2011
date 2- 22/08/2012
Now ,my expected result should be :-
Dec/2011
Jan/2012
Feb/2012
Mar/2012
Apr/2012
May/2012
Jun/2012
Jul/2012
Aug/2012
有人可以帮助我吗?预先感谢。
使用 Joda-Time(由于未指定,我假设你至少可以看看 joda 时间是什么):
LocalDate date1 = new LocalDate("2011-12-12");
LocalDate date2 = new LocalDate("2012-11-11");
while(date1.isBefore(date2)){
System.out.println(date1.toString("MMM/yyyy"));
date1 = date1.plus(Period.months(1));
}
您可以简单地使用 Calendar 类,并通过在每次迭代时添加一个月来从一个日期迭代到另一个日期
using myCalendar.add(Calendar.MONTH, 1);
。
Calendar 类负责避免溢出并为您更新其他字段(此处为年份)。
java.time.YearMonth
虽然其他答案在编写时是很好的答案,但现在已经过时了。自从 Java 8 引入了
YearMonth
类以来,这个任务已经大大简化了:
String date1 = "20/12/2011";
String date2 = "22/08/2012";
DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("dd/MM/uuuu", Locale.ROOT);
DateTimeFormatter monthFormatter = DateTimeFormatter.ofPattern("MMM/uuuu", Locale.ROOT);
YearMonth endMonth = YearMonth.parse(date2, dateFormatter);
for (YearMonth month = YearMonth.parse(date1, dateFormatter);
! month.isAfter(endMonth);
month = month.plusMonths(1)) {
System.out.println(month.format(monthFormatter));
}
打印:
Dec/2011
Jan/2012
Feb/2012
Mar/2012
Apr/2012
May/2012
Jun/2012
Jul/2012
Aug/2012
请为第二次致电
DateTimeFormatter.ofPattern()
提供适当的区域设置,以获取您语言的月份名称。为了安全起见,第一次打电话也是如此。
根据上面的对话。 使用日历和添加方法。
我还没有测试过这个,但它就在那里:
public static List<Date> datesBetween(Date d1, Date d2) {
List<Date> ret = new ArrayList<Date>();
Calendar c = Calendar.getInstance();
c.setTime(d1);
while (c.getTimeInMillis()<d2.getTime()) {
c.add(Calendar.MONTH, 1);
ret.add(c.getTime());
}
return ret;
}
这样做
Calendar cal = Calendar.getInstance();
cal.setTime(your_date_object);
cal.add(Calendar.MONTH, 1);
假设你有这两个约会。您可以通过迭代 while 循环直到 fromDate 早于 toDate 来轻松比较它。
Date fromDate= new Date("1/4/2016");
Date toDate = new Date("31/12/2016");
int i=0;
while(fromDate.before(toDate)){
i=i+1;
fromDate.setMonth(i);
System.out.println(fromDate);
}
您可以在列表中添加日期或做任何想做的事情!!!
现代方法使用 java.time 类。切勿使用
Date
/Calendar
/SimpleDateFormat
等。
LocalDate
LocalDate
类表示仅日期值,没有时间和时区。
LocalDate startLocalDate = LocalDate.of( 2011 , Month.DECEMBER , 20 ) ;
LocalDate stopLocalDate = LocalDate.of( 2012 , Month.AUGUST , 2 ) ;
YearMonth
当仅对年份和月份感兴趣时,请使用该类
YearMonth
。
YearMonth start = YearMonth.from( startLocalDate) ;
YearMonth stop = YearMonth.from( stopLocalDate ) ;
循环。
DateTimeFormatter f = DateTimeFormatter.ofPattern( "MMM/uuuu" , Locale.US ) ;
int initialCapacity = start.until( stop , ChronoUnit.MONTHS ) ;
List< YearMonth > yearMonths = new ArrayList<>( initialCapacity ) ;
YearMonth ym = start ;
while ( ym.isBefore( stop ) ) {
System.out.println( ym.format( f ) ) ; // Output your desired text.
yearMonths.add( ym ) ;
ym = ym.plusMonths( 1 ) ; // Increment to set up next loop.
}
java.time框架内置于 Java 8 及更高版本中。这些类取代了麻烦的旧legacy日期时间类,例如
java.util.Date
、Calendar
和SimpleDateFormat
。
Joda-Time项目现在处于维护模式,建议迁移到java.time类。
要了解更多信息,请参阅 Oracle 教程。并在 Stack Overflow 上搜索许多示例和解释。规格为JSR 310。
您可以直接与数据库交换 java.time 对象。使用符合 JDBC 4.2 或更高版本的 JDBC 驱动程序。不需要字符串,不需要
java.sql.*
类。
从哪里获取java.time类?
ThreeTen-Extra 项目通过附加类扩展了 java.time。该项目是 java.time 未来可能添加的内容的试验场。您可能会在这里找到一些有用的类,例如
Interval
、YearWeek
、YearQuarter
和 more。
java.time
现有答案是正确的。这个答案旨在告知 Joda-Time API 的状态并以不同的方式解决问题。
2014 年 3 月,Java 8 引入了现代日期时间 API
java.time
。从那时起,建议从标准库中的旧版日期时间 API(即 java.util
日期时间 API)和 Joda-Time 切换到 java.time
API。下面是Joda-Time主页上的通知:
请注意,从 Java SE 8 开始,用户被要求迁移到
(JSR-310) - JDK 的核心部分,它将取代它 项目。java.time
此答案中使用的步骤:
LocalDate
模式的
DateTimeFormatter
将给定的日期字符串解析为
dd/MM/uuuu
实例。YearMonth
实例中获取
LocalDate
实例。YearMonth
添加到 List<YearMonth
>。DateTimeFormatter
)的 MMM/uuuu
将上述列表的每个元素映射到字符串中。演示:
public class Main {
public static void main(String[] args) {
DateTimeFormatter parser = DateTimeFormatter.ofPattern("dd/MM/uuuu");
LocalDate date1 = LocalDate.parse("20/12/2011", parser);
LocalDate date2 = LocalDate.parse("22/08/2012", parser);
YearMonth start = YearMonth.from(date1);
YearMonth end = YearMonth.from(date2);
List<YearMonth> list = new ArrayList<>();
// Create a list of desired YearMonth
for (YearMonth ym = start; !ym.isAfter(end); ym = ym.plusMonths(1))
list.add(YearMonth.from(ym));
System.out.println(list);
// In case you need a list of YearMonth in a different string representation
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MMM/uuuu", Locale.ENGLISH);
// The following line of code will run only from Java 16 onwards:
// List<String> formattedStrList1 = list.stream()
// .map(ym -> ym.format(formatter))
// .toList();
//
// In case you are using a version lower than Java 16:
List<String> formattedStrList2 = list.stream()
.map(ym -> ym.format(formatter))
.collect(Collectors.toList());
System.out.println(formattedStrList2);
}
}
输出:
[2011-12, 2012-01, 2012-02, 2012-03, 2012-04, 2012-05, 2012-06, 2012-07, 2012-08]
[Dec/2011, Jan/2012, Feb/2012, Mar/2012, Apr/2012, May/2012, Jun/2012, Jul/2012, Aug/2012]
从 Trail:日期时间了解有关现代日期时间 API 的更多信息。