How to subtract X days from a date using Java calendar?

后端 未结 10 1180
無奈伤痛
無奈伤痛 2020-11-22 15:40

Anyone know a simple way using Java calendar to subtract X days from a date?

I have not been able to find any function which allows me to directly subtract X days fr

相关标签:
10条回答
  • 2020-11-22 15:54

    Taken from the docs here:

    Adds or subtracts the specified amount of time to the given calendar field, based on the calendar's rules. For example, to subtract 5 days from the current time of the calendar, you can achieve it by calling:

    Calendar calendar = Calendar.getInstance(); // this would default to now
    calendar.add(Calendar.DAY_OF_MONTH, -5).
    
    0 讨论(0)
  • 2020-11-22 15:55
    int x = -1;
    Calendar cal = ...;
    cal.add(Calendar.DATE, x);
    

    See java.util.Calendar#add(int,int)

    0 讨论(0)
  • 2020-11-22 15:59

    It can be done easily by the following

    Calendar calendar = Calendar.getInstance();
            // from current time
            long curTimeInMills = new Date().getTime();
            long timeInMills = curTimeInMills - 5 * (24*60*60*1000);    // `enter code here`subtract like 5 days
            calendar.setTimeInMillis(timeInMills);
            System.out.println(calendar.getTime());
    
            // from specific time like (08 05 2015)
            calendar.set(Calendar.DAY_OF_MONTH, 8);
            calendar.set(Calendar.MONTH, (5-1));
            calendar.set(Calendar.YEAR, 2015);
            timeInMills = calendar.getTimeInMillis() - 5 * (24*60*60*1000);
            calendar.setTimeInMillis(timeInMills);
            System.out.println(calendar.getTime());
    
    0 讨论(0)
  • 2020-11-22 16:03

    tl;dr

    LocalDate.now().minusDays( 10 )
    

    Better to specify time zone.

    LocalDate.now( ZoneId.of( "America/Montreal" ) ).minusDays( 10 )
    

    Details

    The old date-time classes bundled with early versions of Java, such as java.util.Date/.Calendar, have proven to be troublesome, confusing, and flawed. Avoid them.

    java.time

    Java 8 and later supplants those old classes with the new java.time framework. See Tutorial. Defined by JSR 310, inspired by Joda-Time, and extended by theThreeTen-Extra project. The ThreeTen-Backport project back-ports the classes to Java 6 & 7; the ThreeTenABP project to Android.

    The Question is vague, not clear if it asks for a date-only or a date-time.

    LocalDate

    For a date-only, without time-of-day, use the LocalDate class. Note that a time zone in crucial in determining a date such as "today".

    LocalDate today = LocalDate.now( ZoneId.of( "America/Montreal" ) );
    LocalDate tenDaysAgo = today.minusDays( 10 );
    

    ZonedDateTime

    If you meant a date-time, then use the Instant class to get a moment on the timeline in UTC. From there, adjust to a time zone to get a ZonedDateTime object.

    Instant now = Instant.now();  // UTC.
    ZoneId zoneId = ZoneId.of( "America/Montreal" );
    ZonedDateTime zdt = ZonedDateTime.ofInstant( instant , zoneId );
    ZonedDateTime tenDaysAgo = zdt.minusDays( 10 );
    


    About java.time

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

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

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

    You may exchange java.time objects directly with your database. Use a JDBC driver compliant with JDBC 4.2 or later. No need for strings, no need for java.sql.* classes.

    Where to obtain the java.time classes?

    • Java SE 8, Java SE 9, Java SE 10, and later
      • Built-in.
      • Part of the standard Java API with a bundled implementation.
      • Java 9 adds some minor features and fixes.
    • Java SE 6 and Java SE 7
      • Much of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport.
    • Android
      • Later versions of Android bundle implementations of the java.time classes.
      • For earlier Android (<26), the ThreeTenABP project adapts ThreeTen-Backport (mentioned above). See How to use ThreeTenABP….

    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.

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