How to add 7 days to current date while not going over available days of a month?

前端 未结 8 1002
长发绾君心
长发绾君心 2020-12-06 17:16

I am trying to get todays day of the month.

And what i want to do is add seven days to the number and the get that current day of the month.

Also i want it t

相关标签:
8条回答
  • 2020-12-06 17:35

    tl;dr

    LocalDate.now( ZoneId.of( "America/Montreal" ) )  // Today’s date.
             .plusWeeks( 1 )   // Yields `LocalDate` object
             .getDayOfMonth()  // Yields `int` number
    

    java.time

    Java 8 and later comes with the java.time framework. These new classes supplant the old java.util.Date/.Calendar classes. For older Android, see the ThreeTen-Backport and ThreeTenABP projects described below.

    These classes include the LocalDate class for when you want a date-only without time-of-day and without time zone. But note that a time zone is crucial in determining the current date as a new day dawns earlier in the east.

    ZoneId zoneId = ZoneId.of( "America/Montreal" );
    LocalDate today = LocalDate.now( zoneId );
    LocalDate weekLater = today.plusWeeks( 1 ); // Automatically rolls over between months, no problem.
    

    If so desired, you can interrogate that LocalDate object for its day-of-month number.

    int dayOfMonth = weekLater.getDayOfMonth();
    

    About java.time

    The java.time framework is built into Java 8 and later. These classes supplant the troublesome old date-time classes such as java.util.Date, .Calendar, & java.text.SimpleDateFormat.

    The Joda-Time project, now in maintenance mode, advises migration to java.time.

    To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations.

    Much of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport and further adapted to Android in ThreeTenABP (see How to use…).

    The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.


    Joda-Time

    UPDATE: The Joda-Time project is now in maintenance mode. The team advises migration to the java.time classes. I leave this section intact as history.

    In Android without Java 8 technology, you can add the Joda-Time library to your project. But know that the Joda-Time project is in maintenance mode and advises migration to java.time classes (see ThreeTenABP above for Android).

    Joda-Time provided the inspiration for java.time. In this case the code needed is quite similar.

    DateTimeZone zone = DateTimeZone.forID( "America/Montreal" );
    LocalDate today = LocalDate.now( zone );
    LocalDate weekLater = today.plusWeeks( 1 ); // Automatically rolls over between months, no problem.
    int dayOfMonth = weekLater.getDayOfMonth();
    
    0 讨论(0)
  • 2020-12-06 17:36
    add(Calendar.DAY_OF_MONTH, 7);
    

    From Calendar JavaDoc

    0 讨论(0)
  • 2020-12-06 17:40

    Calendar's add method does this for you: cal.add(Calendar.DATE, 7);

    EDIT: Given the extended comments, I guess I should add to this by saying that if cal begins as October 4, 2011, and I call cal.add(Calendar.DATE, 7) the new value of cal is October 11, 2011. Similarly, if cal begins as March 29, 2025, then after cal.add(Calendar.DATE, 7) the new value of cal is April 5, 2025.

    0 讨论(0)
  • 2020-12-06 17:44
    Date newDate = new Date(current timestamp in millis + 604800000L);//after 7 days
    
    0 讨论(0)
  • 2020-12-06 17:46
        Date m = new Date();
        Calendar cal = Calendar.getInstance();  
        cal.setTime(m);  
        cal.add(Calendar.DATE, 10); // 10 is the days you want to add or subtract   
        m = cal.getTime();   
        System.out.println(m);
    
    0 讨论(0)
  • 2020-12-06 17:49

    Use GregorianCalendar.add(Calendar.DATE, 7). The GregorianCalendar class will take care of rolling the date into the next month.

    0 讨论(0)
提交回复
热议问题