简体   繁体   中英

How to get year, month, day in UTC?

I have a some Date that was gotten by Calendar.getInstance() (in local time zone as I got).

public final class Converter {

    private static final TimeZone UTC_ZONE = TimeZone.getTimeZone("UTC");

    private Converter() {

    }

    public static int toPackedUTCDate(Date date) {
        Calendar c = Calendar.getInstance(UTC_ZONE);
        c.setTime(date);
        int year = c.get(Calendar.YEAR); // UTC year
        int month = c.get(Calendar.MONTH); // UTC month
        int dayOfMonth = c.get(Calendar.DAY_OF_MONTH); // UTC day of month
        return TimeUtils.packDate(dayOfMonth, month, year);
    }
}

Of course, it's a wrong code but how to get a correct? Cuz date was gotten by Calendar.getInstance().getTime() . Sorry, I really can't get all these time zones... If someone can explain me clearly I will be very grateful. I just need to the user could input his date (in his local time zone) and my app has stored as date in UTC in own packed format.

tl;dr

LocalDate.now( ZoneOffset.UTC )

java.time

You are using troublesome old date-time classes that are now legacy, supplanted by the java.time classes.

LocalDate

The LocalDate class represents a date-only value without time-of-day and without time zone.

Time zone

A time zone is crucial in determining a date. For any given moment, the date varies around the globe by zone. For example, a few minutes after midnight in Paris France is a new day while still “yesterday” in Montréal Québec .

If no time zone is specified, the JVM implicitly applies its current default time zone. That default may change at any moment, so your results may vary. Better to specify your desired/expected time zone explicitly as an argument.

Specify a proper time zone name in the format of continent/region , such as America/Montreal , Africa/Casablanca , or Pacific/Auckland . Never use the 3-4 letter abbreviation such as EST or IST as they are not true time zones, not standardized, and not even unique(!).

ZoneId z = ZoneId.of( "America/Montreal" ) ;  
LocalDate today = LocalDate.now( z ) ;

If you want to use the JVM's current default time zone, ask for it and pass as an argument. If omitted, the JVM's current default is applied implicitly. Better to be explicit.

ZoneId z = ZoneId.systemDefault() ;  // Get JVM’s current default time zone.

If you want UTC, use the constant defined as ZoneOffset.UTC .

LocalDate todayUtc= LocalDate.now( ZoneOffset.UTC ) ;

Sorry, I really can't get all these time zones

These concepts have been covered many many times already on Stack Overflow, sometimes quite in-depth.

Search thoroughly before posting. Search for Instant , ZoneId , ZoneOffset , "java.time", OffsetDateTime , ZonedDateTime , DateTimeFormatter .


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 .

Where to obtain the java.time classes?

    Date date = new Date(year,month,day,hour,min);
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
    sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
    Date utc= new Date(sdf.format(date));

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