Set time to 00:00:00

前端 未结 12 2124
时光说笑
时光说笑 2020-11-29 20:04

I have a problem resetting hours in Java. For a given date I want to set the hours to 00:00:00.

This is my code :

/**
     * Resets milliseconds, se         


        
相关标签:
12条回答
  • 2020-11-29 20:26

    Here are couple of utility functions I use to do just this.

    /**
     * sets all the time related fields to ZERO!
     *
     * @param date
     *
     * @return Date with hours, minutes, seconds and ms set to ZERO!
     */
    public static Date zeroTime( final Date date )
    {
        return DateTimeUtil.setTime( date, 0, 0, 0, 0 );
    }
    
    /**
     * Set the time of the given Date
     *
     * @param date
     * @param hourOfDay
     * @param minute
     * @param second
     * @param ms
     *
     * @return new instance of java.util.Date with the time set
     */
    public static Date setTime( final Date date, final int hourOfDay, final int minute, final int second, final int ms )
    {
        final GregorianCalendar gc = new GregorianCalendar();
        gc.setTime( date );
        gc.set( Calendar.HOUR_OF_DAY, hourOfDay );
        gc.set( Calendar.MINUTE, minute );
        gc.set( Calendar.SECOND, second );
        gc.set( Calendar.MILLISECOND, ms );
        return gc.getTime();
    }
    
    0 讨论(0)
  • 2020-11-29 20:27

    One more JAVA 8 way:

    LocalDateTime localDateTime = LocalDateTime.now().truncatedTo(ChronoUnit.HOURS);
    

    But it's a lot more useful to edit the date that already exists.

    0 讨论(0)
  • 2020-11-29 20:36

    Doing this could be easier (In Java 8)

    LocalTime.ofNanoOfDay(0)
    
    0 讨论(0)
  • 2020-11-29 20:39

    We can set java.util.Date time part to 00:00:00 By using LocalDate class of Java 8/Joda-datetime api:

    Date datewithTime = new Date() ; // ex: Sat Apr 21 01:30:44 IST 2018
    LocalDate localDate = LocalDate.fromDateFields(datewithTime);
    Date datewithoutTime = localDate.toDate(); // Sat Apr 21 00:00:00 IST 2018
    
    0 讨论(0)
  • 2020-11-29 20:40

    As Java8 add new Date functions, we can do this easily.

    
        // If you have instant, then:
        Instant instant1 = Instant.now();
        Instant day1 = instant1.truncatedTo(ChronoUnit.DAYS);
        System.out.println(day1); //2019-01-14T00:00:00Z
    
        // If you have Date, then:
        Date date = new Date();
        Instant instant2 = date.toInstant();
        Instant day2 = instant2.truncatedTo(ChronoUnit.DAYS);
        System.out.println(day2); //2019-01-14T00:00:00Z
    
        // If you have LocalDateTime, then:
        LocalDateTime dateTime = LocalDateTime.now();
        LocalDateTime day3 = dateTime.truncatedTo(ChronoUnit.DAYS);
        System.out.println(day3); //2019-01-14T00:00
        String format = day3.format(DateTimeFormatter.ISO_LOCAL_DATE_TIME);
        System.out.println(format);//2019-01-14T00:00:00
    
    
    
    0 讨论(0)
  • 2020-11-29 20:41

    Another way to do this would be to use a DateFormat without any seconds:

    public static Date trim(Date date) {
        DateFormat format = new SimpleDateFormat("dd.MM.yyyy");
        Date trimmed = null;
        try {
            trimmed = format.parse(format.format(date));
        } catch (ParseException e) {} // will never happen
        return trimmed;
    }
    
    0 讨论(0)
提交回复
热议问题