如何获得一年的开始和结束日期?
我必须使用Java Date
类来解决这个问题(它与我无法控制的东西接口)。
如何获取一年的开始和结束日期,然后遍历每个日期?
Calendar cal = Calendar.getInstance(); cal.set(Calendar.YEAR, 2014); cal.set(Calendar.DAY_OF_YEAR, 1); Date start = cal.getTime(); //set date to last day of 2014 cal.set(Calendar.YEAR, 2014); cal.set(Calendar.MONTH, 11); // 11 = december cal.set(Calendar.DAY_OF_MONTH, 31); // new years eve Date end = cal.getTime(); //Iterate through the two dates GregorianCalendar gcal = new GregorianCalendar(); gcal.setTime(start); while (gcal.getTime().before(end)) { gcal.add(Calendar.DAY_OF_YEAR, 1); //Do Something ... }
java.time
使用Java 8及更高版本中内置的java.time
库。 特别是LocalDate
和TemporalAdjusters
类。
import java.time.LocalDate import static java.time.temporal.TemporalAdjusters.firstDayOfYear import static java.time.temporal.TemporalAdjusters.lastDayOfYear LocalDate now = LocalDate.now(); // 2015-11-23 LocalDate firstDay = now.with(firstDayOfYear()); // 2015-01-01 LocalDate lastDay = now.with(lastDayOfYear()); // 2015-12-31
如果您需要添加时间信息,可以使用任何可用的LocalDate
到LocalDateTime
转换
lastDay.atStartOfDay(); // 2015-12-31T00:00
Calendar cal = new GregorianCalendar(); cal.set(Calendar.DAY_OF_YEAR, 1); System.out.println(cal.getTime().toString()); cal.set(Calendar.DAY_OF_YEAR, 366); // for leap years System.out.println(cal.getTime().toString());
// suppose that I have the following variable as input int year=2011; Calendar calendarStart=Calendar.getInstance(); calendarStart.set(Calendar.YEAR,year); calendarStart.set(Calendar.MONTH,0); calendarStart.set(Calendar.DAY_OF_MONTH,1); // returning the first date Date startDate=calendarStart.getTime(); Calendar calendarEnd=Calendar.getInstance(); calendarEnd.set(Calendar.YEAR,year); calendarEnd.set(Calendar.MONTH,11); calendarEnd.set(Calendar.DAY_OF_MONTH,31); // returning the last date Date endDate=calendarEnd.getTime();
要进行迭代,您应该使用日历对象并增加day_of_month变量
希望它可以提供帮助
我假设您有Date类实例,您需要根据Date类实例查找当前年份的第一个日期和最后日期。 您可以使用Calendar类。 使用提供的日期类实例构造Calendar实例。 将MONTH和DAY_OF_MONTH字段分别设置为0和1,然后使用getTime()方法,该方法将返回表示年份第一天的Date类实例。 您可以使用相同的技术来查找年终。
Date date = new Date(); System.out.println("date: "+date); Calendar cal = Calendar.getInstance(); cal.setTime(date); System.out.println("cal:"+cal.getTime()); cal.set(Calendar.MONTH, 0); cal.set(Calendar.DAY_OF_MONTH, 1); System.out.println("cal new: "+cal.getTime());
对Srini的回答有所改进。
使用Calendar.getActualMaximum
确定一年中的最后一个日期。
Calendar cal = Calendar.getInstance(); calDate.set(Calendar.DAY_OF_YEAR, 1); Date yearStartDate = calDate.getTime(); calDate.set(Calendar.DAY_OF_YEAR, calDate.getActualMaximum(Calendar.DAY_OF_YEAR)); Date yearEndDate = calDate.getTime();
如果你正在寻找一个单行表达式,我通常使用这个:
new SimpleDateFormat("yyyy-MM-dd").parse(String.valueOf(new java.util.Date().getYear())+"-01-01")
您可以使用Jodatime,如此线程中所示.Java Joda Time – 实现日期范围迭代器
此外,您可以使用格里高利历并一次移动一天,如此处所示。 我需要一个迭代日期间隔的循环
PS。 建议:先搜索一下。
您可以使用具有DateUtils类的apache commons-lang项目。
它们提供了一个迭代器,您可以为其提供Date对象。
但我强烈建议使用其他答案所建议的Calendar类。
更新: Joda-Time库现在处于维护模式,其团队建议迁移到java.time类。 请参阅Przemek的正确java.time答案 。
时区
其他答案忽略了时区的关键问题。
乔达时间
避免使用臭名昭着的java.util.Date类进行日期时间工作。 而是使用Joda-Time或java.time。 根据需要转换为juDate对象以实现互操作性。
DateTimeZone zone = DateTimeZone.forID( "America/Montreal" ) ; int year = 2015 ; DateTime firstOfYear = new DateTime( year , DateTimeConstants.JANUARY , 1 , 0 , 0 , zone ) ; DateTime firstOfNextYear = firstOfYear.plusYears( 1 ) ; DateTime firstMomentOfLastDayOfYear = firstOfNextYear.minusDays( 1 ) ;
转换为java.util.Date
根据需要转换为juDate。
java.util.Date d = firstOfYear.toDate() ;
GregorianCalendar gcal = new GregorianCalendar(); gcal.setTime(start); while (gcal.getTime().before(end)) { gcal.add(Calendar.DAY_OF_YEAR, 1); //Do Something ... }
这里的GregorianCalendar创作毫无意义。 事实上,通过Calendar.java源代码显示Calendar.getInstance()已经提供了GregorianCalendar实例。
此致,尼古拉斯
Calendar cal = Calendar.getInstance();//getting the instance of the Calendar using the factory method we have a get() method to get the specified field of the calendar ie year int year=cal.get(Calendar.YEAR);//for example we get 2013 here cal.set(year, 0, 1); setting the date using the set method that all parameters like year ,month and day Here we have given the month as 0 ie Jan as the month start 0 - 11 and day as 1 as the days starts from 1 to30. Date firstdate=cal.getTime();//here we will get the first day of the year cal.set(year,11,31);//same way as the above we set the end date of the year Date lastdate=cal.getTime();//here we will get the last day of the year System.out.print("the firstdate and lastdate here\n");
一年的第一天和最后一天
import java.util.Calendar import java.text.SimpleDateFormat val parsedDateInt = new SimpleDateFormat("yyyy-MM-dd") val cal2 = Calendar.getInstance() cal2.add(Calendar.MONTH, -(cal2.get(Calendar.MONTH))) cal2.set(Calendar.DATE, 1) val firstDayOfYear = parsedDateInt.format(cal2.getTime) cal2.add(Calendar.MONTH, (11-(cal2.get(Calendar.MONTH)))) cal2.set(Calendar.DATE, cal2.getActualMaximum(Calendar.DAY_OF_MONTH)) val lastDayOfYear = parsedDateInt.format(cal2.getTime)