I have been using the legacy Date API to calculate time difference between two times in yyyyMMdd'T'HHmmss format.
Client sets the requestExpirationTime attribute in request and server has a validation check to ensure that "RequestExpirationTime is not before current system time".
Client:
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd'T'HHmmss");
Timestamp originatingTimestamp = new Timestamp(new Date().getTime());
// duration is offset 5minutes for exmaple
Timestamp requestExpirationTimestamp = new Timestamp(originatingTimestamp.getTime() + duration);
request.setRequestExpirationTimestamp(simpleDateFormat.format(requestExpirationTimestamp));
Server:
try {
Date systemTime = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd'T'HHmmss");
String ret = request.getRequestExpirationTimestamp();
if (ret != null) {
Date requestExpirationTimestamp = sdf.parse(ret);
if (requestExpirationTimestamp.before(systemTime)) {
logger.info("Request timedout: RequestExpirationTime is before current system time");
}
}
}
This code is giving issues as the systemTime on server comes in IST (Indian Standard Time) and client can send in any UTC/IST.
I have now moved to Java 8, so I can leave the legacy Date Api in Java.
How can I ensure such Timezone issues don't come in my code ? I tried setting timeZone in
SimpleDateFormat on the server but the new Date() is behaving differently on different servers.
Any help is appreciated.
When the client sends time that is not in UTC, for example India Standard Time (IST), and the server doesn’t know which time zone the client used, then there is nothing the server can do to make it work correctly. Instead I suggest that you make sure that the client sends time in UTC always.
Client
Make sure you always send time in UTC:
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("uuuuMMdd'T'HHmmss");
String requestExpirationTime = OffsetDateTime.now(ZoneOffset.UTC)
.plusMinutes(5)
.format(formatter);
System.out.println(requestExpirationTime);
Example output from running just now:
20200506T183409
Server
Now the server knows the time is in UTC. I am assuming that it is using a formatter identical to the one the client used.
OffsetDateTime dateTime = LocalDateTime.parse(requestExpirationTime, formatter)
.atOffset(ZoneOffset.UTC);
OffsetDateTime systemTime = OffsetDateTime.now(ZoneOffset.UTC);
if (dateTime.isBefore(systemTime)) {
System.out.println("Request timedout: RequestExpirationTime is before current system time");
} else {
System.out.println("OK");
}
OK
Tip
You may append Z to your timestamp string to make it explicit that it is UTC. It might prevent some mistakes further down the road.
20200506T183409Z
Related
I'm parsing the times from AD. There are two formats, i.e., YMD LDAP timestamps for whenCreated, whenChanged, 18-digit LDAP/FILETIME timestamps for lastLogonTimestamp, pwdLastSet, etc. Because I need to analyze the data upon the time. It makes sense to get local time. Here are the two functions that I wrote to parse the two different formats. The calculation in the second function I referenced from Convert 18-digit LDAP Timestamps To Human Teadable Date Using Java
public static String parseLdapDate(String ldapDate) {
String[] parts = ldapDate.split("[.]");
String dateTimePart = parts[0]; //take the date string before .0Z
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddhhmmss");
sdf.setTimeZone(TimeZone.getTimeZone("GMT")); //Z means UTC time, to the local timezone
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try {
Date tempDate = sdf.parse(dateTimePart); //parse the string to a date
return formatter.format(tempDate); //format it as what we want
} catch (ParseException ex) {
System.out.println("Parsing LDAP Date exception \n");
ex.printStackTrace();
}
return null;
}
public static String parseLdapTimestamp(String ldapTimestamp) {
long nanoseconds = Long.parseLong(ldapTimestamp); // 100 nanoseconds
long mills = (nanoseconds/10000000); // To seconds
long unix = (((1970-1601)*365)-3+Math.round((1970-1601)/4))*86400L;
long timeStamp = mills - unix;
Date date = new Date(timeStamp*1000L); // To milliseconds
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
return sdf.format(date);
}
I have an example 20150520143936.0Z, which is converted to "2015-05-20 16:39:36".
For the example 131097986571852097, it is converted to "2016-06-07 18:44:17", while http://www.epochconverter.com/ldap tells me that it's the GMT time and the local time is "2016-06-07 20:44:17". I will get the local time if I comment the code of setting timezone.
So now I'm confused, sdf.setTimeZone(TimeZone.getTimeZone("GMT")); gives me local timezone or the universal time. I was thinking if AD stores whenCreated in the universal time, lastLogonTimestamp in local time. But in the functions I parse them as strings. There is no symbol about the timezone. If I comment this sentence in the second function, will I get local time for both attributes when I access an LDAP Directory in another place.
In the second case, you're constructing a Date and then telling it to format that date in UTC - whereas in the first case, you're parsing it in UTC, but formatting it in local time. You're already assuming that the timestamp is stored as a number of ticks since the Unix epoch, which is a time zone neutral format.
If the aim is to produce a string representation in local time, then you should remove the sdf.setTimeZone call. I would argue that a parse method should be returning a Date anyway though, rather than a String. Or better yet, return a java.time.Instant...
In my Oracle DB date is stored in Central Time. My local machine is also in Central time. I am trying to write a Java code so that DB central time can be converted to any local machine time zone. For example if I change my local machine time in Eastern time time from DB will be converted to Eastern time.
Here is Java method which takes date from DB and offset minutes from user browser using JavaScript
private String convertDateToLocal(Date createDate, Long offSetMins) {
DateFormat formatter = new SimpleDateFormat("MM/dd/yyyy hh:mm a", Locale.US);
Calendar calendar = Calendar.getInstance();
calendar.setTime(createDate);
Long dbTimeInMilliSec = createDate.getTime();
Long dbTimeOffSet = (long) (calendar.getTimeZone().getOffset(dbTimeInMilliSec));
Long dbTimeToUTC = dbTimeInMilliSec + (dbTimeOffSet * -1);
Long dbTimeToLocal = dbTimeToUTC - (offSetMins * 60 * 1000);
Date createDateInLocal = new Date(dbTimeToLocal);
return formatter.format(createDateInLocal);
}
Just for reference this is how I am getting offset minutes using JavaScript
var offSetMins= new Date().getTimezoneOffset();
But the issue is even when I have local machine setup to Central time I am seeing time 1hr ahead for days where there is no daylight saving.
DB date (11/26/2014 12:03 PM) is showing as (11/26/2014 01:03 PM) in UI.
Dates in daylight saving time is OK.
DB date (05/20/2015 05:08 PM) is showing as (05/20/2015 05:08 PM) in UI.
I used jsTimezoneDetect in front end and Joda-Time in backend Java code to solve this issue.
JavaScript
var tz = jstz.determine();
var timeZone = tz.name();
//send this timeZone to server
Java Code
private String convertDateToLocal(Date createDate, String timeZone) {
Long dbDate = createDate.getTime();
DateTimeFormatter fmt = DateTimeFormat.forPattern("MM/dd/yyyy hh:mm a zz");
DateTime origDate = new DateTime(dbDate);
DateTime dtTz = origDate.withZone(DateTimeZone.forID(timeZone));
return dtTz.toString(fmt);
}
Try using Timezone class of JAVA.
You need to have two timezone object one for DB Timezone and second one for machine timezone and then you can do the conversions.
hope this helps
Kaushik
This question already has answers here:
Timezone conversion
(13 answers)
Closed 8 years ago.
I want to create an alarm application for FIFA 2014 world cup matches in which i have a server which stores the date and time for the matches in Brazil/Acre and my client is an android application and an android device may have any of the possible timezone so the my problem is i want to convert the Brazil/Acre timing to the local android device timing with different timezone and after lot of googled i came to know about joda data and time lib but it is too slow in android so please suggest any code that will work for me.
In my opinion Time class is the best for your job. Also it is Android API not general Java API.
Here I mentioned some of useful methods for your job:
void switchTimezone(String timezone)
Convert this time object so the time represented remains the same, but is instead located in a different timezone.
static String getCurrentTimezone()
Returns the timezone string that is currently set for the device.
And if you want to save a time in a timezone independed manner, you can convert to milliseconds (in UTC) by toMillis() method and then retrieve it by set(long millis) method.
If something is unclear please tell me!
UPDATE
Example:
long timeMillis = /* get time milliseconds form the server */
Time time = new Time();
time.set(timeMillis);
/* changing time zone */
time.switchTimezone(/* your desired timezone in string format */);
/* getting time as string */
String timeString = time.format("%Y%m%dT%H%M%S"); // you can change format as you wish
Here is a table for formatting times
You could use this code, which substracts the hour-difference between Brazil and the local timezone. Just replace yourDate with a Date-object.
//code...
yourDate.setTime(yourDate.getTime() - getDifferenceInMillis());
//code...
public int getDifferenceInMillis() {
// Local Time
int localMinute = Calendar.getInstance().get(Calendar.MINUTE);
int localHour = Calendar.getInstance().get(Calendar.HOUR_OF_DAY);
int localDay = Calendar.getInstance().get(Calendar.DAY_OF_MONTH);
// Brazil Time
Calendar c = new GregorianCalendar(TimeZone.getTimeZone("Brazil/Acre"));
c.setTimeInMillis(new Date().getTime());
int brazilMinute = c.get(Calendar.MINUTE);
int brazilHour = c.get(Calendar.HOUR_OF_DAY);
int brazilDay = c.get(Calendar.DAY_OF_MONTH);
// Difference between Brazil and local
int minuteDifference = brazilMinute - localMinute;
int hourDifference = brazilHour - localHour;
int dayDifference = brazilDay - localDay;
if (dayDifference != 0) {
hourDifference = hourDifference + 24;
}
return (hourDifference * 60 + minuteDifference) * 60 * 1000;
}
You should store your date has a long or timestamp in your server. If you don't want you can anyway send your date as a long generate from your database date. Then create your calendar instance like that :
Calendar c = Calendar.getInstance();
c.setTime(new Date(yourdatelong));
You can have to multiplie and add some constant in your long. In java the definition is "the number of milisecond since 1970 1/1 00:00:00". It is differents in C#, for exemple ( number of nanoseconde from 1/1/1900, if I remenber well).
Like that you are sure to set the same date in all your device. When it is done, you just have to put the timezone that you want in your calendar to display the local time.
http://developer.android.com/reference/java/util/Calendar.html
You can have many option to manage time and display it in this class.
If dates are stored in server time zone (Brazil/Acre), you should load date time from DB, convert it to UTC time zone and send to client. On client side change UTC to local time zone:
Server side:
DateTime dateOnServer = // load date from db
DateTime dateUTC = dateOnServer.withZone(DateTimeZone.UTC); // convert to UTC
String dateAsStringUTC = dateUTC.toString("yyyy-MM-ddTHH:mm:ss");
// send 'dateAsStringUTC' to client
Client side:
String dateAsStringUTC = // receive date from server
DateTimeFormatter dtf = DateTimeFormat.forPattern("yyyy-MM-ddTHH:mm:ss"); // parser for date
DateTime dateOnClient= dtf.parseDateTime(dateAsStringUTC);
// 'dateOnClient' will be in client time zone 'DateTimeZone.getDefault()'
I faced same problem like you...
I got the solution using SimpleDateFormat
SimpleDateFormat sourceFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
sourceFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
Date parsed = format.parse("2011-03-01 15:10:37"); // => Date is in UTC now
TimeZone tz = TimeZone.getTimeZone("America/Chicago");
SimpleDateFormat destFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
destFormat.setTimeZone(tz);
String result = destFormat.format(parsed);
this may help you..
I want to show server date time to client side which has same status of server datetime. This date is used to achieve some schedule-wise feature. I do following steps:
Fetched initial date time from server and just calculated time
date.getTime() difference.
Add time diff to client datetime
Update client time using Timer of GWT
It is working fine when server timezone and client timezone both are same. But it gives me wrong calculation with different timezone.
Just example:
if my client time is Indian timezone
((UTC+05:30) Chennai, Kolkata, Mumbai, New Delhi)
and
sever timezone is
((UTC-08:00) Pacific Time (US & Canada))
so it calculates wrong server date.
how can I show current server time with different timezone of server and client without making frequently server request?
Note: The solution should be universal timezone.
Edit:
I used RPC Mechanism and returning new Date().getTime() from server to client.
And in success method, The client side code:
final String serverDate;
final DateTimeFormat fmt = DateTimeFormat.getFormat(dateFormat);
if(dateFormat!=null){
serverDate = fmt.format(result.getServerDate());
}else{
serverDate = result.getServerDate().toString();
}
setDateTime(serverDate,widget);
final long dateDiff = result.getServerDate().getTime()-new Date().getTime();
Timer timer = new Timer() {
#Override
public void run() {
long currenrDate=new Date().getTime()+dateDiff;
Date date=new Date(currenrDate);
String serverDate = fmt.format(date);
setDateTime(serverDate,widget);
WorkFlowSessionFactory.putValue(WorkFlowSesisonKey.SERVER_DATE_TIME,date);
}
};
timer.scheduleRepeating(10000);
Did you try some thing like this?
SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
f.setTimeZone(TimeZone.getTimeZone("UTC"));
Date date=new Date();
System.out.println(f.format(date)); // current UTC time
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.add(Calendar.HOUR_OF_DAY, 5);
cal.add(Calendar.MINUTE, 30);
System.out.println(f.format(cal.getTime()));// current client side time in UTC
Calendar cal2 = Calendar.getInstance();
cal2.setTime(date);
cal2.add(Calendar.HOUR_OF_DAY, -8);
cal2.add(Calendar.MINUTE, 00);
System.out.println(f.format(cal2.getTime()));// current server side time in UTC
I have a MySql database that stores a timestamp for each record I insert. I pull that timestamp into my Android application as a string. My database is located on a server that has a TimeZone of CST. I want to convert that CST timestamp to the Android device's local time.
Can someone help with this?
Use getTimeZone.getDefault combined with according to the Android documentation.
public static synchronized TimeZone
getDefault ()
Gets the default time zone. Returns
the default time zone.
So since you know that CST is -6:00 from GMT, and you get a local timezone saying the user is +9:00 (Japan), you'd know to adjust your MySQL DB times by +15 hours (9 - (-6)). Or if they are in Miami (EST, -5), you would adjust by adding one hour (-5 - (-6)). If the are in Portland, Oregon, (PST -8), you would subtract 2 hours (-8 -(-6)).
So really you just need to get the local timezone offset and feed it into the basic equation: TimeZone.getDefault + 6 and you'll know what to add or subtract to your local DB. (+6 since -(-6) always works out to +6).
If I knew the first thing about writing Java, I'd go the extra step and write a bit of sample code, but alas, I'm only smart enough for scripts.
Crude Attempt at Java
I already said I have no idea how to do Java or object oriented anything, right?
Here's a crude attempt from just poking around the Android documentation. Any fine points or simple "Not even close" remarks welcome. Bear in mind that I figured out the right method and class already just from a quick search and I came up with a simple equation for converting the timezone offset for anywhere to CST, so I'm not a dunce, just someone who doesn't know when to leave well enough alone. Anyway, crude attempt:
System now = System.currentTimeMillis (); //Gets current local time in ms
TimeZone local_tz = TimeZone.getDefault(); //Gets current local TZ of phone
tz_offset_gmt = local_tz.getOffset(now)/3600000; // Get Offset in ms, divide by 3600000
tz_offset_cst = tz_offset_gmt + 6; // add offset to 6 to get current TZ offset to CST.
Anywhere close to how to do this in java?
Suppose you have a string of date in CST, parse it with timezone CST and then format it with the default timezone on your android.
String s = "2011-01-01 12:00:00";
DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
df.setTimeZone(TimeZone.getTimeZone("CST"));
Date timestamp = null;
try {
timestamp = df.parse(s);
df.setTimeZone(TimeZone.getDefault());
System.out.println(df.format(timestamp));
} catch (ParseException e) {
e.printStackTrace();
}
can't you simply convert the date with simpleDateFormat?
then you just define the structure of your incoming date like that (df) and transform it to the form you want (df):
private static DateFormat df = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss Z");
private static DateFormat df2 = new SimpleDateFormat("dd/MM/yyyy 'at' HH:mm");
public void setyourDate(String yourDate) {
Date date2;
yourDate = getyourDate() + "" + yourDate;
try {
date2 = df.parse(yourDate);
yourDate = df2.format(date2);
} catch (ParseException e) {
}
this.yourDate = yourDate;
}
does it make sense?
This is an old question, but I want to write my answer. Assume, the timestamp you get from SQL is like the following format: yyyy-MM-dd'T'HH:mm:ss
public static Date convertStringToDate(String strDate) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
sdf.setTimeZone(TimeZone.getTimeZone("CST"));
return sdf.parse(strDate);
}