How to get start and end date of a year?

前端 未结 15 1759
清酒与你
清酒与你 2021-02-05 02:57

I have to use the Java Date class for this problem (it interfaces with something out of my control).

How do I get the start and end date of a year and then

相关标签:
15条回答
  • 2021-02-05 03:22
        // 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();
    

    To iterate, you should use the calendar object and increment the day_of_month variable

    Hope that it can help

    0 讨论(0)
  • 2021-02-05 03:23

    First and Last day of Year

    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)
    
    0 讨论(0)
  • 2021-02-05 03:23
    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)
    
    0 讨论(0)
  • 2021-02-05 03:24
    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 讨论(0)
  • 2021-02-05 03:24
     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 讨论(0)
  • 2021-02-05 03:24

    Update: The Joda-Time library is now in maintenance mode with its team advising migration to the java.time classes. See the correct java.time Answer by Przemek.

    Time Zone

    The other Answers ignore the crucial issue of time zone.

    Joda-Time

    Avoid doing date-time work with the notoriously troublesome java.util.Date class. Instead use either Joda-Time or java.time. Convert to j.u.Date objects as needed for interoperability.

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

    Convert To java.util.Date

    Convert to j.u.Date as needed.

    java.util.Date d = firstOfYear.toDate() ;
    
    0 讨论(0)
提交回复
热议问题