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
Related
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
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 am using following Java code to get current date and time:
DateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy");
DateFormat timeFormat = new SimpleDateFormat("HH:mm:ss");
Date date = new Date();
System.out.println(dateFormat.format(date));
System.out.println(timeFormat.format(date));
The Output Time is always 2 hours ahead of my system time or my Time Zone. For example if above code outputs following time:
11:44:43
Then my system's time is:
09:44:43
Please help me out to solve this problem!
Try this:
DateFormat timeFormat = new SimpleDateFormat("HH:mm:ss");
TimeZone tz = TimeZone.getDefault();
Calendar c = Calendar.getInstance(tz);
Date date = c.getTime();
System.out.println(timeFormat.format(date));
If it's not working, try to change the time zone. Take a look at TimeZone.getTimeZone(String id).
your problem is Timezone
for exp.
this code for android.
change timezone of app.
because android default timezone is "UTC"
SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss");
formatter.setTimeZone(TimeZone.getTimeZone("UTC"));
I have a date that I get from a server formatted in EST like this
05/07/2012 16:55:55 goes month/day/year then time
if the phone is not in EST how can I convert it to the timezone the phone is in?
it would be not problem if I got the time in milliseconds but I dont
EDIT:
ok now the time is not correct when formatting
String sTOC = oNewSTMsg.getAttribute("TOC").toString();
String timezoneID = TimeZone.getDefault().getID();
DateFormat format = new SimpleDateFormat("MM/dd/yy HH:mm:ss");
format.setTimeZone(TimeZone.getTimeZone("EST"));
String newtimezoneID = TimeZone.getDefault().getID();
Date timestamp = null;
try{
timestamp = format.parse(sTOC);
format.setTimeZone(TimeZone.getDefault());
timezoneID = format.format(timestamp);
}catch(ParseException e){
}
I convert it to "EST" then format that time to the default TimeZone but the time is always off by an hour, not sure why?
Use the following code to get a UNIX timestamp:
String serverResp = "05/07/2012 16:55:55";
DateFormat format = new SimpleDateFormat("MM/dd/yy HH:mm:ss");
Date date = format.parse(serverResp);
Now you have the timestamp, which you know how to use.
Here's another question which covers conversion, in case you are curious: Android Convert Central Time to Local Time
Use the DateFormat class to parse the String into a Date. See the introduction to the API document here... http://docs.oracle.com/javase/1.5.0/docs/api/java/text/DateFormat.html
You can then create a Calendar for the Date...
Calendar cal = Calendar.getInstance().setTime(date);
And then you can change the timezone on the Calendar to a different timezone using setTimezone(). Or just get the time in milliseconds, using getTimeInMillis()
Using the Calendar, Date, and DateFormat classes should put you in the right direction.
See the Calendar documentation here... http://docs.oracle.com/javase/1.5.0/docs/api/java/util/Calendar.html