简体   繁体   中英

JUnit test cases for converting UTC to EST

I have tried writing a class to convert from UTC to EST time zone. Also, I want only the date part to get stored. It is working properly. Now I am looking for various JUnit test cases for the method dateFromUtcToJavaTimeZone. Can you give me any suggestions on those?

package com.ssc.dmh.util.general;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;

public class DateUtil {

    public static Date dateFromUtcToJavaTimeZone(Date date) {
        if (date == null) {
            return null;
        }
        String utcDateString = utcDateString(date);
        return com.ssc.dmh.util.DateUtil.stringToDate(utcDateString, "yyyy-MM-dd");
    }

    private static String utcDateString(Date date) {
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
        return dateFormat.format(date);
    }
}

tl;dr

Assert:

myJavaUtilDate                       // An object of type `java.util.Date`, a terrible legacy class that is no longer used.
.toInstant()                         // Convert from legacy class `java.util.Date` to modern `java.time.Instant`. Both classes represent a moment in UTC.
.atZone(                             // Adjust from UTC to a time zone.
    ZoneId.of( "America/New_York" )  // Real time zone names are in format `Continent/Region`, never 2-4 letter pseudo-zones such as `EST`, `CST`, or `IST`. 
)                                    // Returns a `ZonedDateTime` object. Same moment, different wall-clock time.
.toLocalDate()                       // Extract the date only.
.toString()                          // Generate text representing that date, in standard ISO 8601 format.
.equals(
    "2020-01-23" 
)

Avoid legacy date-time classes

You are using terrible classes that were supplanted years ago by the modern java.time classes with the adoption of JSR 310.

LocalDate

For a date-only value without time-of-day and without time zone, use java.time.LocalDate .

Your input string complies with the ISO 8601 standard. The java.time classes use the standard formats by default when parsing/generating strings. So no need for a formatting pattern.

LocalDate ld = LocalDate.parse( "2020-01-23" ) ; 

Converting legacy objects

If given a java.util.Date by old code not yet updated to java.time , convert. Call new conversion methods added to the old classes.

Instant instant = myJavaUtilDate.toInstant() ;

Both Instant and Date represent a moment in UTC. That is, both have an offset of zero hours-minutes-seconds.

For any given moment, the date varies around the globe by zone. It is “tomorrow” in Japan while still “yesterday” in Mexico. So you must specify the time zone by which you want to view the date.

ZoneId z = ZoneId.of( "America/New_York" ) ;
ZonedDateTime zdt = instant.atZone( z ) ;

Extract the date-only value.

LocalDate ld = zdt.toLocalDate() ;

Generate text, and compare in your assertion.

String output = ld.toString() ;  // Genetate text in standard ISO 8601 format. 

Java 中的日期时间类型表,现代和传统

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