Changing Java Date one hour back

后端 未结 11 1538
名媛妹妹
名媛妹妹 2020-11-30 20:47

I have a Java date object:

Date currentDate = new Date();

This will give the current date and time. Example:

Thu Jan 12 10:         


        
相关标签:
11条回答
  • 2020-11-30 21:21

    This can be achieved using java.util.Date. The following code will subtract 1 hour from your date.

    Date date = new Date(yourdate in date format);
    Date newDate = DateUtils.addHours(date, -1)
    

    Similarly for subtracting 20 seconds from your date

    newDate = DateUtils.addSeconds(date, -20)    
    
    0 讨论(0)
  • 2020-11-30 21:24

    Get the time in milliseconds, minus your minutes in milliseconds and convert it to Date. Here you need to objectify one!!!

        int minutes = 60;
        long currentDateTime = System.currentTimeMillis();
        Date currentDate = new Date(currentDateTime - minutes*60*1000);
        System.out.println(currentDate);
    
    0 讨论(0)
  • 2020-11-30 21:32

    tl;dr

    In UTC:

    Instant.now().minus( 1 , ChronoUnit.HOURS ) 
    

    Or, zoned:

    Instant.now()
           .atZone( ZoneId.of ( "America/Montreal" ) )
           .minusHours( 1 )
    

    Using java.time

    Java 8 and later has the new java.time framework built-in.

    Instant

    If you only care about UTC (GMT), then use the Instant class.

    Instant instant = Instant.now ();
    Instant instantHourEarlier = instant.minus ( 1 , ChronoUnit.HOURS );
    

    Dump to console.

    System.out.println ( "instant: " + instant + " | instantHourEarlier: " + instantHourEarlier );
    

    instant: 2015-10-29T00:37:48.921Z | instantHourEarlier: 2015-10-28T23:37:48.921Z

    Note how in this instant happened to skip back to yesterday’s date.

    ZonedDateTime

    If you care about a time zone, use the ZonedDateTime class. You can start with an Instant and the assign a time zone, a ZoneId object. This class handles the necessary adjustments for anomalies such as Daylight Saving Time (DST).

    Instant instant = Instant.now ();
    ZoneId zoneId = ZoneId.of ( "America/Montreal" );
    ZonedDateTime zdt = ZonedDateTime.ofInstant ( instant , zoneId );
    ZonedDateTime zdtHourEarlier = zdt.minus ( 1 , ChronoUnit.HOURS );
    

    Dump to console.

    System.out.println ( "instant: " + instant + "\nzdt: " + zdt + "\nzdtHourEarlier: " + zdtHourEarlier );
    

    instant: 2015-10-29T00:50:30.778Z

    zdt: 2015-10-28T20:50:30.778-04:00[America/Montreal]

    zdtHourEarlier: 2015-10-28T19:50:30.778-04:00[America/Montreal]

    Conversion

    The old java.util.Date/.Calendar classes are now outmoded. Avoid them. They are notoriously troublesome and confusing.

    When you must use the old classes for operating with old code not yet updated for the java.time types, call the conversion methods. Here is example code going from an Instant or a ZonedDateTime to a java.util.Date.

    java.util.Date date = java.util.Date.from( instant );
    

    …or…

    java.util.Date date = java.util.Date.from( zdt.toInstant() );
    

    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, Java SE 11, and later - 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
      • Most 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)
  • 2020-11-30 21:33

    Use Calendar.

    Calendar cal = Calendar.getInstance();
    
    cal.setTime(new Date());
    cal.set(Calendar.HOUR, cal.get(Calendar.HOUR) - 1);
    
    0 讨论(0)
  • 2020-11-30 21:35

    Similar to @Sumit Jain's solution

    Date currentDate = new Date(System.currentTimeMillis() - 3600 * 1000);
    

    or

    Date currentDate = new Date(System.currentTimeMillis() - TimeUnit.HOURS.toMillis(1));
    
    0 讨论(0)
  • 2020-11-30 21:37

    Or using the famous Joda Time library:

    DateTime dateTime = new DateTime();
    dateTime = dateTime.minusHours(1);
    Date modifiedDate = dateTime.toDate();
    
    0 讨论(0)
提交回复
热议问题