Skip to content

Instantly share code, notes, and snippets.

@Feldstrom
Last active November 4, 2019 18:38
Show Gist options
  • Save Feldstrom/9ed355dc0f3add901e2bf5dc15252c13 to your computer and use it in GitHub Desktop.
Save Feldstrom/9ed355dc0f3add901e2bf5dc15252c13 to your computer and use it in GitHub Desktop.
Unix2Date_New
public static Date PSTDate2UTC_New( Date dtDate) {
if(dtDate==null) return null;
SimpleDateFormat sdfPST= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
sdfPST.setTimeZone(TimeZone.getTimeZone("PST8PDT"));
LocalDateTime localtDateAndTime = LocalDateTime.parse(sdfPST.format(dtDate), DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
ZoneId oZoneId = ZoneId.of("America/Los_Angeles");
ZonedDateTime zdt = ZonedDateTime.of(localtDateAndTime, oZoneId );
System.out.println("Unconverted date and time in PST: " + zdt);
ZonedDateTime utcDate = zdt.withZoneSameInstant(ZoneOffset.UTC);
String sDT_UTC=DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss").format(utcDate);
System.out.println("Converted date and time in UTC sDT_UTC=" + sDT_UTC);
//localtDateAndTime = LocalDateTime.parse(sDT_UTC, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
SimpleDateFormat sdfUTC= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
sdfUTC.setTimeZone(TimeZone.getTimeZone("UTC"));
Date dtResult=null;
try {
dtResult=sdfUTC.parse(sDT_UTC);
} catch (ParseException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
return dtResult;
}
public static Date Unix2Date_New( String sDate) {
Date dtReturn=new Date();
if(sDate==null) return dtReturn;
long longUnix=Long.parseLong(sDate);
dtReturn=new Date(longUnix);
SimpleDateFormat sdfGMT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
sdfGMT.setTimeZone(TimeZone.getTimeZone("UTC"));
SimpleDateFormat sdfPST = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
sdfPST.setTimeZone(TimeZone.getTimeZone("America/Los_Angeles"));
SimpleDateFormat sdfResult = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'"); //default tz
//sdfResult.setTimeZone(TimeZone.getTimeZone("America/Los_Angeles"));
//SimpleDateFormat sdfResult = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'000Z'"); //default tz
String sGMT=sdfGMT.format(dtReturn);
System.out.println("sdfGMT="+sGMT);
String sPST = sdfPST.format(dtReturn);
System.out.println("sPST="+sPST);
String sDefault = sdfResult.format(dtReturn);
System.out.println("sDefault="+sDefault);
Date dtTemp_PST=null;
Instant oTimestamp =Instant.ofEpochMilli(longUnix);
String sInstance=oTimestamp.toString();
System.out.println("sInstance="+sInstance);
try {
dtTemp_PST=sdfResult.parse(sInstance);
} catch (ParseException e) {
e.printStackTrace();
}
return dtTemp_PST;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment