Get Today's date in Java at midnight time

前端 未结 12 1958
北海茫月
北海茫月 2020-12-09 07:08

I need to create two date objects. If the current date and time is March 9th 2012 11:30 AM then

  • date object d1 should be 9th March 20
相关标签:
12条回答
  • 2020-12-09 07:44

    Here is a Java 8 based solution, using the new java.time package (Tutorial).

    If you can use Java 8 objects in your code, use LocalDateTime:

    LocalDateTime now = LocalDateTime.now(); // current date and time
    LocalDateTime midnight = now.toLocalDate().atStartOfDay();
    


    If you require legacy dates, i.e. java.util.Date:

    Convert the LocalDateTime you created above to Date using these conversions:

    LocalDateTime -> ZonedDateTime -> Instant -> Date

    1. Call atZone(zone) with a specified time-zone (or ZoneId.systemDefault() for the system default time-zone) to create a ZonedDateTime object, adjusted for DST as needed.

      ZonedDateTime zdt = midnight.atZone(ZoneId.of("America/Montreal"));
      
    2. Call toInstant() to convert the ZonedDateTime to an Instant:

      Instant i = zdt.toInstant()
      
    3. Finally, call Date.from(instant) to convert the Instant to a Date:

      Date d1 = Date.from(i)
      


    In summary it will look similar to this for you:

    LocalDateTime now = LocalDateTime.now(); // current date and time
    LocalDateTime midnight = now.toLocalDate().atStartOfDay();
    Date d1 = Date.from(midnight.atZone(ZoneId.systemDefault()).toInstant());
    Date d2 = Date.from(now.atZone(ZoneId.systemDefault()).toInstant());
    


    See also section Legacy Date-Time Code (The Java™ Tutorials) for interoperability of the new java.time functionality with legacy java.util classes.

    0 讨论(0)
  • 2020-12-09 07:53
    DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");    
    Date date = new Date(); System.out.println(dateFormat.format(date));    //2014/08/06 15:59:4
    
    0 讨论(0)
  • private static Date truncateTime(Calendar cal) {
            cal.set(Calendar.HOUR_OF_DAY, 0);
            cal.set(Calendar.MINUTE, 0);
            cal.set(Calendar.SECOND, 0);
            cal.set(Calendar.MILLISECOND, 0);
            return new Date(cal.getTime().getTime());
    }
     public static void main(String[] args) throws Exception{
            Date d2 = new Date();
            GregorianCalendar cal = new GregorianCalendar();
            cal.setTime(d2);
            Date d1 = truncateTime( cal );
            System.out.println(d1.toString());
            System.out.println(d2.toString());
    }
    
    0 讨论(0)
  • 2020-12-09 08:02
        Calendar c = new GregorianCalendar();
        c.set(Calendar.HOUR_OF_DAY, 0); //anything 0 - 23
        c.set(Calendar.MINUTE, 0);
        c.set(Calendar.SECOND, 0);
        Date d1 = c.getTime(); //the midnight, that's the first second of the day.
    

    should be Fri Mar 09 00:00:00 IST 2012

    0 讨论(0)
  • 2020-12-09 08:04

    If you are able to add external libs to your project. I would recommend that you try out Joda-time. It has a very clever way of working with dates.

    http://joda-time.sourceforge.net/

    0 讨论(0)
  • 2020-12-09 08:06
    Date date = new Date();
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm");
    System.out.println(sdf.format(date));
    
    0 讨论(0)
提交回复
热议问题