2012-02-21 67 views
17

我必须使用Java Date类来解决这个问题(它与我的控制之外的东西接口)。如何获取一年的开始和结束日期?

如何获取一年的开始日期和结束日期,然后遍历每个日期?

+2

日期被删除。改用日历。 – 2012-02-21 18:23:05

+6

那么,'Calendar'不是一个选项? @Stas:这不是真的。一堆弃用的方法不会使整个类不被使用。 – BalusC 2012-02-21 18:23:18

+0

@BalusC:“在JDK 1.1之前,Date类有两个额外的功能,它允许将日期解释为年,月,日,小时,分和秒值,还允许格式化和解析日期字符串。不幸的是,这些函数的API不适用于国际化,从JDK 1.1开始,Calendar类应该用于在日期和时间字段之间进行转换,DateFormat类应该用于格式化和解析日期字符串。已弃用。“ – 2012-02-21 18:26:53

回答

32
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 ... 
} 
+0

啊,我没有意识到你可以在日历和日期之间进行转换! – 2012-02-23 14:35:34

+2

你不要在它们之间转换;日期代表即时时间,而日历会告诉您该时刻在特定系统中的表现。 – PaulJWilliams 2013-02-15 10:26:46

+6

此代码可以通过诸如'cal.set(Calendar.MONTH,Calendar.DECEMBER)的常量来改进;' – tos 2013-11-27 09:49:52

4
// 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变量

希望它可以帮助

+0

你可以用今年的日子来代替。 – 2012-02-21 18:37:14

+0

@ srini.venigalla:true – VirtualTroll 2012-02-21 18:39:45

+0

啊,我没意识到你可以在日历和日期之间进行转换! – 2012-02-23 14:35:48

1

我假设你有Date类的实例,你需要找到根据Date类实例计算当前的第一个日期和最后一个日期。你可以使用Calendar类来做到这一点。使用提供的日期类实例构造日历实例。将MONTH和DAY_OF_MONTH字段分别设置为0和1,然后使用getTime()方法,该方法将返回表示Date类的第一天的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()); 
0

严重,只是谷歌搜索“今年java的第一天,”让我this link,使用日历构造一个很好的例子。

不知道你为什么要使用日期,除非你受到限制,这是某种家庭作业。

+0

API限制我无法解决,感谢这个想法! – 2012-02-23 14:35:13

+0

链接已死: – 2014-07-17 11:53:08

+0

链接已更新,感谢您的支持! – Carlos 2014-07-17 22:16:27

5
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()); 
+0

这应该是公认的答案 – mithrandir 2014-12-07 16:46:41

+1

这是非闰年的工作吗?还是应该手动纠正? – Davor 2015-10-28 14:53:10

+0

请记住,有些年份会有365天和其他人将有366. 这种方法不应该使用。 – 2017-06-19 14:28:38

0

您可以使用具有DateUtils类的apache commons-lang项目。

它们提供了一个迭代器,你可以给Date对象。

但我强烈建议使用Calendar类作为其他答案的建议。

-1
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 i.e 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 i.e 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 first day of the year 

System.out.print("the firstdate and lastdatehere\n"); 
+1

可能你解释你的代码,请吗?目前没有什么帮助。 – kleinfreund 2013-02-07 12:06:28

-1
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实例。

问候, 萨科

0

更新:的乔达时库现在处于维护模式,其团队建议迁移到java.time类。看到正确的java.time Answer by Przemek

时区

其他答案忽略了时区的关键问题。

乔达时间

避免做日期时间工作,出了名的麻烦java.util.Date类。而是使用Joda-Time或java.time。根据需要转换为j.u.Date对象以实现互操作性。

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

根据需要转换为j.u.Date。

java.util.Date d = firstOfYear.toDate() ; 
24

java.time

使用java.time库内置到Java 8和更高版本。具体为LocalDateTemporalAdjusters类。

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 

如果您需要添加的时间信息,您可以如果你正在寻找一个1行的表达式中使用任何可用的LocalDateLocalDateTime转换像

lastDay.atStartOfDay(); // 2015-12-31T00:00 
+0

好的答案。我建议总是将想要的/预期的时区传递给' LOCALDATE的。现在(ZoneId)'而不是隐式地依赖于JVM的当前默认时区,该时区在运行时可以随时改变。 'LocalDate.now(ZoneId.of(“America/Montreal”))'同样,总是将'ZoneId'传递给'atStartOfDay'方法,而不是隐式地依赖于JVM默认值。 'lastDay.atStartOfDay(ZoneID.of(“America/Montreal”))' – 2017-05-07 01:22:33

1

,我通常使用这样的:

new SimpleDateFormat("yyyy-MM-dd").parse(String.valueOf(new java.util.Date().getYear())+"-01-01") 
0

对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(); 
相关问题