TimeZone DST issue Date CET, IST to UTC - Yash-777/LearnJava GitHub Wiki
Time zone IST
India Standard Time (IST), UTC +5:30
No daylight saving time, same UTC offset all yea
world clock widget dates for Short Day and Long Day
Central European Time (CET), used in most parts of Europe and a few North African countries, is a standard time which is 1 hour ahead of Coordinated Universal Time (UTC). The time offset from UTC can be written as UTC+01:00.
Currently Central European Summer Time (CEST), UTC +2 Standard time (Central European Time (CET), UTC +1) starts October 25, 2020
CET alternates between UTC+1 (standard time) and UTC+2 (when daylight saving time (DST) is observed). All countries in the CET time zone observe DST (UTC+2) from 02:00 am on the last Sunday of March until 03:00 am on the last Sunday of October
CET
CEST = CETDST
SQL UTC data conventions
SELECT
pa_date.timezone (to_timestamp('2020-03-29T03:00Z', 'yyyy-mm-dd"T"hh24:mi"Z"'), 'UTC', 'CET') Utc_Cet,
pa_date.timezone (to_timestamp('2020-03-29T03:00', 'yyyy-mm-dd"T"hh24:mi'), 'CET', 'UTC') Cet_Utc
FROM dual;
-- 29-MAR-2020 05:00:00 29-MAR-2020 01:00:00
Java
Caused by: org.xml.sax.SAXException: Invalid DateFormat in Payload and should in Format:(yyyy-MM-dd'T'HH:mm). Unparseable date: "2020-03-29T02:00:00Z"
java.text.ParseException: Unparseable date: "2020-03-29T02:00:00Z"
SOL: dateFormat.setLenient(true);
public class ShortDayTest {
public static void main(String[] args) throws ParseException {
String[] datesArr = {
"2020-03-29T12:00:00", "2020-03-29T01:00:00", "2020-03-29T02:00:00", "2020-03-29T03:00:00", "2020-03-29T04:00:00", // Short Day on 2nd hour
"2020-10-25T12:00:00", "2020-10-25T01:00:00", "2020-10-25T02:00:00", "2020-10-25T03:00:00" // Long Day on 2nd hour
};
String timeZone = System.getProperty("user.timezone");
System.out.println("Time Zone:"+timeZone);
//TimeZone.setDefault( TimeZone.getTimeZone("UTC") ); // Default: java.sql.Timestam. Change: GMT, CET, UTC
//System.setProperty("user.timezone", "UTC"); // or -Duser.timezone=GMT in the JVM args.
System.out.println("Zone System:"+ System.getProperty("user.timezone")+", TimeZone.getDefault().toZoneId(): "+ZoneId.systemDefault());
String formatStr = "yyyy-MM-dd'T'HH:mm:ss";
for (String strDate : datesArr) {
DateFormat dateFormat = new SimpleDateFormat(formatStr);
dateFormat.setTimeZone(TimeZone.getTimeZone("UTC")); // Provided date as CET, Converting corresponding Default UTC date
Date dates = dateFormat.parse(strDate);
String utcDate = dateFormat.format(dates);
/*SimpleTimeZone[id=Europe/Berlin,dstSavings=3600000,useDaylight=true]
1603591200000 : UTC TimeStamp: 2020-10-25 02:00:00.0
1603587600 : CET
3600 : dstSavings*/
Calendar cal = new GregorianCalendar();
cal.setTimeZone(TimeZone.getTimeZone("UTC"));
cal.setTimeInMillis(dates.getTime());
DateFormat dateFormat22 = new SimpleDateFormat(formatStr);
dateFormat22.setLenient(true);
//dateFormat22.setTimeZone(TimeZone.getTimeZone("CET")); // UTC, -01:00, GMT, GMT-7, IST-"Asia/Kolkata"
Date dates22 = dateFormat22.parse(utcDate);
// Don’t use GMT-7 as a time zone. Use for example America/Los_Angeles. Of course select the time zone that makes sense for your situation.
// https://www.epochconverter.com/ - Sun, 29 Mar 2020 02:00:00 GMT, 1585447200000 = 1585447200000
//System.out.println("Timestamp in milliseconds:"+ dates.getTime() + ", Epoch Time:"+dates22.getTime()/ 1000);
/*System.out.println("UTC - :"+utcDate+" CET2UTC:"+dates22+" TimeStamp UTC: "+ new java.sql.Timestamp(dates22.getTime())+
"TimeStamp CET: "+ new java.sql.Timestamp(dates22.getTime()));*/
DateFormat dateFormat2 = new SimpleDateFormat(formatStr, Locale.ENGLISH);
dateFormat2.setTimeZone(TimeZone.getTimeZone("CET"));
Date dates2 = dateFormat2.parse(strDate);
//System.out.format("UTC - [%s] = [%s] GMT:%s\n", strDate, dates2, dates2.toGMTString());
displayKeyVal("Privided", strDate);
displayKeyVal("Locale.CET", dates22+"");
/*
displayKeyVal("convertUTCtoCET", convertUTCtoCET(strDate) );
displayKeyVal("Locale.UTC", dates+"");
displayKeyVal("UTC Str", utcDate);
displayKeyVal("GMT String", dates.toGMTString());
displayKeyVal("Locale.CET", dates22+"");
//displayKeyVal("Locale.ENGLISH", dates2+"");
//displayKeyVal("UTC Time Default(CET)", new java.sql.Timestamp(dates.getTime())+"");
//displayKeyVal("UTC - CET Time", new java.sql.Timestamp(dates22.getTime())+"");
//displayKeyVal("ENGLISH Time", new java.sql.Timestamp(dates2.getTime())+"");
//displayKeyVal("GregorianCalendar", cal.getTime()+"");
//displayKeyVal("TimeStamp GC", new java.sql.Timestamp(cal.getTimeInMillis())+"");
//displayKeyVal("TimeStamp Local", (getTimestamp(strDate)).toString());
*/ System.out.println("======================");
}
/*String startDate = "2020-03-29T01:00:00";
String endDate = "2020-03-29T02:00:00";
compareTwoTimeStamps(startDate, endDate);
String startDate2 = "2020-03-29T03:00:00";
String endDate2 = "2020-03-29T04:00:00";
compareTwoTimeStamps(startDate2, endDate2);
System.out.println("------------------------------");
try {
lenientStrictParseDate(dateStr, "UTC");
} catch (ParseException e) {
e.printStackTrace();
}
try {
parseDate(dateStr);
} catch (ParseException e) {
e.printStackTrace();
}
Instant instant = Instant.parse( dateStr + "Z"); // `Instant` is always in UTC.
java.util.Date date = java.util.Date.from( instant );
System.out.println("UTC : "+date);
String prop = System.getProperty("java.time.zone.DefaultZoneRulesProvider");
System.out.println("DefaultZoneRulesProvider : "+ prop);
displayZonedDate(dateStr, "UTC"); // UTC, GMT, UT
displayZonedDate(dateStr, "CET");
//displayZonedDate(dateStr, "CETDST");
// displayZoneRules();
*/
}
public static Timestamp getTimestamp(String strDate) {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss");
LocalDateTime dateTime = LocalDateTime.parse(strDate, formatter);
//LocalDateTime.of(10000, Month.DECEMBER, 30, 12, 10, 05);
Timestamp valueOf = java.sql.Timestamp.valueOf(dateTime);
System.out.format("TimeStamp Local : [%-30s]\n", valueOf.toString());
java.sql.Date sqlDate = new java.sql.Date( valueOf.getTime() );
System.out.format("java.sql.Date : [%-30s]\n", sqlDate);
return valueOf;
/*LocalDateTime with = dateTime.with(TemporalAdjusters.lastInMonth(DayOfWeek.SUNDAY));
int dayOfMonth = with.getDayOfMonth();
System.out.println("Last Sunday of Month: "+dayOfMonth);*/
}
public static void displayKeyVal(String key, String val) {
System.out.format("[%-25s] : [%-35s]\n", key, val);
}
public static long compareTwoTimeStamps(String startDate, String endDate) throws ParseException {
Date start = parseDate(startDate);
Date end = parseDate(endDate);
java.sql.Timestamp startTime = getTimeStamp(start);
java.sql.Timestamp endTime = getTimeStamp(end);
long startTimeMilli = startTime.getTime();
long endTimeMilli = endTime.getTime();
long diff = endTimeMilli - startTimeMilli;
//long diffSeconds = diff / 1000;
//long diffMinutes = diff / (60 * 1000);
long diffHours = diff / (60 * 60 * 1000);
//long diffDays = diff / (24 * 60 * 60 * 1000);
System.out.format("Start:%s, End:%s\n", start, end);
System.out.println("Difference in Hours : "+ diffHours);
if (diffHours >= 1) {
Timestamp oneHourAgo = new Timestamp(endTime.getTime() - (60 * 60 * 1000));
System.out.println("--- Before: "+ endTime.toLocaleString());
System.out.println("--- After: "+ oneHourAgo.toLocaleString());
}
return diffHours;
}
public static void displayZonedDate(String dateStr, String zone) {
Instant instant = Instant.parse( dateStr + "Z");
ZoneId zoneId = ZoneId.of( zone ); // Define a time zone rather than rely implicitly on JVM’s current default time zone.
ZonedDateTime zdt = ZonedDateTime.ofInstant( instant , zoneId ); // Assign a time zone adjustment from UTC.
java.util.Date dateZoned = java.util.Date.from( zdt.toInstant() );
System.out.println(zone+" : "+dateZoned);
}
public static Timestamp getTimeStamp(Date date) { //Date dbResponseTime = new java.util.Date();
Timestamp timestamp = new java.sql.Timestamp(date.getTime());
System.out.println("Timestamp :"+ timestamp);
return timestamp;
}
private static String convertUTCtoCET(String UTCDate) throws ParseException {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
Date date = formatter.parse(UTCDate);
SimpleDateFormat FORMATTER = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String dateValue = FORMATTER.format(date);
SimpleDateFormat DF = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
DF.setLenient(true);
DF.setTimeZone(TimeZone.getTimeZone("UTC"));
SimpleDateFormat DF1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date startDate = DF.parse(dateValue);
DF1.setTimeZone(TimeZone.getTimeZone("CET"));
String CETDateFormat = DF1.format(startDate);
return CETDateFormat;
}
static String formatStr = "yyyy-MM-dd'T'HH:mm:ss";
public static Date lenientStrictParseDate(String aSource, String zone) throws ParseException {
DateFormat dateFormat = new SimpleDateFormat(formatStr);
dateFormat.setTimeZone(TimeZone.getTimeZone(zone));
//dateFormat.setLenient(false);
Date date = dateFormat.parse(aSource);
System.out.format(zone + " Old [%s]= New Date [%s]\n", aSource, date.toGMTString());
getTimeStamp(date);
return date;
}
public static Date parseDate(String aSource) throws ParseException {
DateFormat dateFormat = new SimpleDateFormat(formatStr, Locale.ENGLISH);
Date date = dateFormat.parse(aSource);
System.out.format("attribute [%s]= Date [%s]\n", aSource, date.toString());
getTimeStamp(date);
return date;
}
public static void displayZoneRules() {
ZoneId zone = ZoneId.of("CET");
System.out.println(zone);
System.out.println(zone.getRules());
for (ZoneOffsetTransition trans : zone.getRules().getTransitions()) {
System.out.println(trans);
}
for (ZoneOffsetTransitionRule rule : zone.getRules().getTransitionRules()) {
System.out.println(rule);
}
}
}