简体   繁体   中英

create Date object from year, month, day AND time

Is it possible to create a java.util.Date object from year, month, day AND time?

I am using this code:

private Date convertToDate(LocalTime time)
{
    @SuppressWarnings("deprecation")
    Instant instant = time.atDate(LocalDate.of(date.getYear(),
           date.getMonth(), date.getDay())).atZone(ZoneId.systemDefault()).toInstant();
    return Date.from(instant);
}

The problem I am facing is that this code always adds 6 minutes and 32 seconds to my time.
So if my LocalTime time is eg 08:00 the created Date is Mon Sep 03 08:06:32 CET 115 .

You are using Date#getYear wrongly:

Returns a value that is the result of subtracting 1900 from the year that contains or begins with the instant in time represented by this Date object, as interpreted in the local time zone.

getYear() has an offset of 1900, which you have to add when using LocalDate.of(year, month, day) . That's why the year of your calculated Instant is 115.

Instant instant = time.atDate(LocalDate.of(date.getYear() + 1900,
       date.getMonth(), date.getDay())).atZone(ZoneId.systemDefault()).toInstant();

You can try to use Calendar class

Calendar calendar = Calendar.getInstance();
calendar.clear();
calendar.set(Calendar.MONTH, month);
calendar.set(Calendar.YEAR, year);
Date date = calendar.getTime();

If you want to set the date and the time, use the LocalDateTime class.

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;
import java.util.concurrent.TimeUnit;

public class Example {
    public static void main(String[] args) {
        long hours = TimeUnit.MILLISECONDS.convert(18, TimeUnit.HOURS);
        long minutes = TimeUnit.MILLISECONDS.convert(30, TimeUnit.MINUTES);
        long seconds = TimeUnit.MILLISECONDS.convert(42, TimeUnit.SECONDS);
        long time = hours + minutes + seconds; // 6:30:42 PM
        Date date = toDate(2015, 10, 13, time);
        DateFormat dateFmt = new SimpleDateFormat("MMM d yyyy  hh:mm:ss a");

        System.out.println(dateFmt.format(date)); // Oct 13 2015  06:30:42 PM
    }

    public static Date toDate(int year, int month, int date, long time) {
        int hour = (int) TimeUnit.MILLISECONDS.toHours(time) % 24;
        int minute = (int) TimeUnit.MILLISECONDS.toMinutes(time) % 60;
        int second = (int) TimeUnit.MILLISECONDS.toSeconds(time) % 60;
        int milli = (int) TimeUnit.MILLISECONDS.toMillis(time);

        return toDate(year, month, date, hour, minute, second, milli);
    }

    public static Date toDate(int year, int month, int date) {
        return toDate(year, month, date, 0);
    }

    public static Date toDate(int year, int month, int date, int hour) {
        return toDate(year, month, date, hour, 0);
    }

    public static Date toDate(int year, int month, int date, int hour, int minute) {
        return toDate(year, month, date, hour, minute, 0);
    }

    public static Date toDate(int year, int month, int date, int hour, int minute, int second) {
        return toDate(year, month, date, hour, minute, second, 0);
    }

    public static Date toDate(int year, int month, int date, int hour, int minute, int second, int milli) {
        return toDate(LocalDateTime.of(year, month, date, hour, minute, second, milli));
    }

    public static Date toDate(LocalDateTime timestamp) {
        return Date.from(timestamp.atZone(ZoneId.systemDefault()).toInstant());
    }
}

Here is an alternative solution using a one-liner without deprecated methods:

return Date.from(date.toInstant()
                     .atZone(ZoneId.systemDefault())
                     .with(time)
                     .toInstant());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM