如何获得一年的开始和结束日期?

时间:2012-02-21 18:18:48

标签: java date

我必须使用Java Date类来解决这个问题(它与我无法控制的东西接口)。

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

14 个答案:

答案 0 :(得分:36)

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 ...
}

答案 1 :(得分: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());

答案 2 :(得分: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变量

希望它可以提供帮助

答案 3 :(得分:2)

如果您正在寻找单行表达式,我通常会使用它:

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

答案 4 :(得分:1)

我假设您有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());

答案 5 :(得分:1)

对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();

答案 6 :(得分:0)

您可以使用此线程中显示的Jodatime Java Joda Time - Implement a Date range iterator

此外,您可以使用格里高利历并一次移动一天,如下所示。 I need a cycle which iterates through dates interval

PS。建议:先搜索一下。

答案 7 :(得分:0)

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

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

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

答案 8 :(得分:0)

更新: Joda-Time库现在处于维护模式,其团队建议迁移到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() ;

答案 9 :(得分:0)

年份的第一天和最后一天

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)

答案 10 :(得分:0)

val instance = Calendar.getInstance()
                instance.add(Calendar.YEAR,-1)

 val prevYear = SimpleDateFormat("yyyy").format(DateTime(instance.timeInMillis).toDate())

val firstDayPreviousYear = DateTime(prevYear.toInt(), 1, 1, 0, 0, 0, 0)

val lastDayPreviousYear = DateTime(prevYear.toInt(), 12, 31, 0, 0, 0, 0)

答案 11 :(得分:0)

java.time.YearMonth

如何获取特定年份和月份的第一个日期和最后一个日期。

这是使用 YearMonth 类的代码。

YearMonthjava.time 包中的最终类,在 Java 8 中引入。

public static void main(String[] args) {

    int year = 2021;  // you can pass any value of year Like 2020,2021...
    int month = 6;   // you can pass any value of month Like 1,2,3...
    YearMonth yearMonth = YearMonth.of( year, month );  
    LocalDate firstOfMonth = yearMonth.atDay( 1 );
    LocalDate lastOfMonth = yearMonth.atEndOfMonth();

    System.out.println(firstOfMonth);
    System.out.println(lastOfMonth);
}

看到这个code run live at IdeOne.com

<块引用>

2021-06-01

2021-06-30

答案 12 :(得分:-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 last day of the year

System.out.print("the firstdate and lastdate here\n");

答案 13 :(得分:-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实例。

此致 尼古拉斯

相关问题