Created
February 20, 2020 15:31
-
-
Save gelin/e40cd5ef61f6ab5a809963f602ecc67a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.junit.Test; | |
import java.text.DateFormat; | |
import java.util.Calendar; | |
import java.util.Date; | |
import java.util.Locale; | |
import java.util.TimeZone; | |
import static org.junit.Assert.assertEquals; | |
public class DateTimeFormatTest { | |
static Date makeDateTime(TimeZone timeZone, int year, int month, int day, int hour, int minute) { | |
Calendar calendar = Calendar.getInstance(timeZone); // need timezone to correctly interpret input numbers | |
calendar.setTimeInMillis(0L); // just to zero seconds and milliseconds | |
calendar.set(year, month - 1, day, hour, minute); // calendar months start from zero | |
return calendar.getTime(); // here we get only the long number | |
} | |
static long makeTimestamp(TimeZone timeZone, int year, int month, int day, int hour, int minute) { | |
Calendar calendar = Calendar.getInstance(timeZone); // need timezone to correctly interpret input numbers | |
calendar.setTimeInMillis(0L); // just to zero seconds and milliseconds | |
calendar.set(year, month - 1, day, hour, minute); // calendar months start from zero | |
return calendar.getTimeInMillis(); // here we get only the long number | |
} | |
static String formatDateTime(Date dateTime, TimeZone timeZone, Locale locale, int dateStyle, int timeStyle) { | |
DateFormat format = DateFormat.getDateTimeInstance(dateStyle, timeStyle, locale); | |
format.setTimeZone(timeZone); // timezone makes sense again | |
return format.format(dateTime); | |
} | |
static String formatDateTime(Date dateTime, TimeZone timeZone, Locale locale) { | |
return formatDateTime(dateTime, timeZone, locale, DateFormat.LONG, DateFormat.LONG); | |
} | |
static String formatDateTime(Date dateTime, TimeZone timeZone) { | |
return formatDateTime(dateTime, timeZone, Locale.US); | |
} | |
static String formatTimestamp(long timestamp, TimeZone timeZone, Locale locale, int dateStyle, int timeStyle) { | |
DateFormat format = DateFormat.getDateTimeInstance(dateStyle, timeStyle, locale); | |
format.setTimeZone(timeZone); // timezone makes sense again | |
return format.format(new Date(timestamp)); // Date is just a timestamp | |
} | |
static String formatTimestamp(long timestamp, TimeZone timeZone, Locale locale) { | |
return formatTimestamp(timestamp, timeZone, locale, DateFormat.LONG, DateFormat.LONG); | |
} | |
static String formatTimestamp(long timestamp, TimeZone timeZone) { | |
return formatTimestamp(timestamp, timeZone, Locale.US); | |
} | |
@Test | |
public void testMakeDateTime() { | |
Date epoch = makeDateTime(TimeZone.getTimeZone("UTC"), 1970, 1, 1, 0, 0); | |
assertEquals(0L, epoch.getTime()); | |
Date happydev = makeDateTime(TimeZone.getTimeZone("Asia/Omsk"), 2015, 12, 5, 10, 20); | |
assertEquals(1449289200000L, happydev.getTime()); | |
} | |
@Test | |
public void testFormatDateTime() { | |
Date epoch = makeDateTime(TimeZone.getTimeZone("UTC"), 1970, 1, 1, 0, 0); | |
assertEquals("January 1, 1970 12:00:00 AM UTC", formatDateTime(epoch, TimeZone.getTimeZone("UTC"))); | |
assertEquals("January 1, 1970 6:00:00 AM OMST", formatDateTime(epoch, TimeZone.getTimeZone("Asia/Omsk"))); | |
assertEquals("December 31, 1969 7:00:00 PM EST", formatDateTime(epoch, TimeZone.getTimeZone("EST"))); | |
Date happydev = makeDateTime(TimeZone.getTimeZone("Asia/Omsk"), 2015, 12, 5, 10, 20); | |
assertEquals("December 5, 2015 4:20:00 AM UTC", formatDateTime(happydev, TimeZone.getTimeZone("UTC"))); | |
assertEquals("December 5, 2015 10:20:00 AM OMST", formatDateTime(happydev, TimeZone.getTimeZone("Asia/Omsk"))); | |
assertEquals("December 4, 2015 11:20:00 PM EST", formatDateTime(happydev, TimeZone.getTimeZone("EST"))); | |
} | |
@Test | |
public void testMakeTimestamp() { | |
long epoch = makeTimestamp(TimeZone.getTimeZone("UTC"), 1970, 1, 1, 0, 0); | |
assertEquals(0L, epoch); | |
long happydev = makeTimestamp(TimeZone.getTimeZone("Asia/Omsk"), 2015, 12, 5, 10, 20); | |
assertEquals(1449289200000L, happydev); | |
} | |
@Test | |
public void testFormatTimestamp() { | |
long epoch = makeTimestamp(TimeZone.getTimeZone("UTC"), 1970, 1, 1, 0, 0); | |
assertEquals("January 1, 1970 12:00:00 AM UTC", formatTimestamp(epoch, TimeZone.getTimeZone("UTC"))); | |
assertEquals("January 1, 1970 6:00:00 AM OMST", formatTimestamp(epoch, TimeZone.getTimeZone("Asia/Omsk"))); | |
assertEquals("December 31, 1969 7:00:00 PM EST", formatTimestamp(epoch, TimeZone.getTimeZone("EST"))); | |
long happydev = makeTimestamp(TimeZone.getTimeZone("Asia/Omsk"), 2015, 12, 5, 10, 20); | |
assertEquals("December 5, 2015 4:20:00 AM UTC", formatTimestamp(happydev, TimeZone.getTimeZone("UTC"))); | |
assertEquals("December 5, 2015 10:20:00 AM OMST", formatTimestamp(happydev, TimeZone.getTimeZone("Asia/Omsk"))); | |
assertEquals("December 4, 2015 11:20:00 PM EST", formatTimestamp(happydev, TimeZone.getTimeZone("EST"))); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment