Changing Java Date one hour back

后端 未结 11 1539
名媛妹妹
名媛妹妹 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:40

    You can use from bellow code for date and time :

    DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
           //get current date time with Calendar()
           Calendar cal = Calendar.getInstance();
           System.out.println("Current Date Time : " + dateFormat.format(cal.getTime()));
    
           cal.add(Calendar.DATE, 1);
           System.out.println("Add one day to current date : " + dateFormat.format(cal.getTime()));
    
           cal = Calendar.getInstance();
           cal.add(Calendar.MONTH, 1);
           System.out.println("Add one month to current date : " + dateFormat.format(cal.getTime()));
    
           cal = Calendar.getInstance();
           cal.add(Calendar.YEAR, 1);
           System.out.println("Add one year to current date : " + dateFormat.format(cal.getTime()));
    
           cal = Calendar.getInstance();
           cal.add(Calendar.HOUR, 1);
           System.out.println("Add one hour to current date : " + dateFormat.format(cal.getTime()));
    
           cal = Calendar.getInstance();
           cal.add(Calendar.MINUTE, 1);
           System.out.println("Add one minute to current date : " + dateFormat.format(cal.getTime()));
    
           cal = Calendar.getInstance();
           cal.add(Calendar.SECOND, 1);
           System.out.println("Add one second to current date : " + dateFormat.format(cal.getTime()));
    
           cal = Calendar.getInstance();
           cal.add(Calendar.DATE, -1);
           System.out.println("Subtract one day from current date : " + dateFormat.format(cal.getTime()));
    
           cal = Calendar.getInstance();
           cal.add(Calendar.MONTH, -1);
           System.out.println("Subtract one month from current date : " + dateFormat.format(cal.getTime()));
    
           cal = Calendar.getInstance();
           cal.add(Calendar.YEAR, -1);
           System.out.println("Subtract one year from current date : " + dateFormat.format(cal.getTime()));
    
           cal = Calendar.getInstance();
           cal.add(Calendar.HOUR, -1);
           System.out.println("Subtract one hour from current date : " + dateFormat.format(cal.getTime()));
    
           cal = Calendar.getInstance();
           cal.add(Calendar.MINUTE, -1);
           System.out.println("Subtract one minute from current date : " + dateFormat.format(cal.getTime()));
    
           cal = Calendar.getInstance();
           cal.add(Calendar.SECOND, -1);
           System.out.println("Subtract one second from current date : " + dateFormat.format(cal.getTime()));
    

    Output :

    Current Date Time : 2008/12/28 10:24:53
    Add one day to current date : 2008/12/29 10:24:53
    Add one month to current date : 2009/01/28 10:24:53
    Add one year to current date : 2009/12/28 10:24:53
    Add one hour to current date : 2008/12/28 11:24:53
    Add one minute to current date : 2008/12/28 10:25:53
    Add one second to current date : 2008/12/28 10:24:54
    Subtract one day from current date : 2008/12/27 10:24:53
    Subtract one month from current date : 2008/11/28 10:24:53
    Subtract one year from current date : 2007/12/28 10:24:53
    Subtract one hour from current date : 2008/12/28 09:24:53
    Subtract one minute from current date : 2008/12/28 10:23:53
    Subtract one second from current date : 2008/12/28 10:24:52
    

    This link is good : See here

    And see : See too

    And : Here

    And : Here

    And : Here

    If you need just time :

    DateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
    
    0 讨论(0)
  • Just subtract the number of milliseconds in an hour from the date.

    currentDate.setTime(currentDate.getTime() - 3600 * 1000));

    0 讨论(0)
  • 2020-11-30 21:42

    It worked for me instead using format .To work with time just use parse and toString() methods

    String localTime="6:11"; LocalTime localTime = LocalTime.parse(localtime)

    LocalTime lt = 6:11; localTime = lt.toString()

    0 讨论(0)
  • 2020-11-30 21:43

    To subtract hours, you need to use the HOUR_OF_DAY constant. Within that, include the number with the negative sign. This would be the hours you want to reduce. All this is done under the Calendar add() method.

    The following is an example:

    import java.util.Calendar;
    public class Example {
      public static void main(String[] args) {
       Calendar c = Calendar.getInstance();
         System.out.println("Date : " + c.getTime());
           // 2 hours subtracted
       c.add(Calendar.HOUR_OF_DAY, -2);
       System.out.println("After subtracting 2 hrs : " + c.getTime());
     }
    }
    

    Here is the output:

    Date : Sun Dec 16 16:28:53 UTC 2018
    After subtracting 2 hrs : Sun Dec 16 14:28:53 UTC 2018
    
    0 讨论(0)
  • 2020-11-30 21:45

    java.util.Calendar

    Calendar cal = Calendar.getInstance();
    // remove next line if you're always using the current time.
    cal.setTime(currentDate);
    cal.add(Calendar.HOUR, -1);
    Date oneHourBack = cal.getTime();
    

    java.util.Date

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

    org.joda.time.LocalDateTime

    new LocalDateTime().minusHours(1)
    

    Java 8: java.time.LocalDateTime

    LocalDateTime.now().minusHours(1)
    

    Java 8 java.time.Instant

    // always in UTC if not timezone set
    Instant.now().minus(1, ChronoUnit.HOURS));
    // with timezone, Europe/Berlin for example
    Instant.now()
           .atZone(ZoneId.of("Europe/Berlin"))
           .minusHours(1));
    
    0 讨论(0)
提交回复
热议问题