I am getting a little crazy with date and time libraries in Java. Basically what I need is to get two String dates, the first one corresponds to the previous day in Australia time (taking into account daylight time) with a specific hour (will come as a parameter) and with the following date format: "yyyy-MM-dd'T'HH:mm:ss.SSS zzz".
Consider I will receive the time as a string like the following: "180000". So if today is Nov-17 in Australia, I will need to get the following String:
"2017-11-16T18:00:00.000 AEDT"
And the second string date I need is the same day as today in au time:
"2017-11-17T18:00:00.000 AEDT"
I am not able to use JDK 8, I must use JDK 7.
I tried different ways but I am not getting what I need. Any suggestion will be great.
I saw your comment that you cannot use Java 8, you must use JDK 7. At the same time, especially for not quite trivial run-of-the-mill operations like yours, java.time, the modern Java date and time API that came out with Java 8 in 2014 is so much nicer to work with. Fortunately the modern API has been backported to Java 6 and 7 too, in the ThreeTen Backport (that’s ThreeTen for JSR-310, where the API was first described). So I encourage you to get the backport and start coding:
ZoneId australianTime = ZoneId.of("Australia/Sydney");
DateTimeFormatter receivedTimeFormat = DateTimeFormatter.ofPattern("HHmmss");
DateTimeFormatter neededDateTimeFormatter
= DateTimeFormatter.ofPattern("uuuu-MM-dd'T'HH:mm:ss.SSS zzz",
Locale.forLanguageTag("en-AU"));
LocalDate today = LocalDate.now(australianTime);
LocalDate yesterday = today.minusDays(1);
String receivedTimeString = "180000";
LocalTime time = LocalTime.parse(receivedTimeString, receivedTimeFormat);
String previousDayAtTime = yesterday.atTime(time)
.atZone(australianTime)
.format(neededDateTimeFormatter);
System.out.println(previousDayAtTime);
When I ran this snippet today, it printed the requested output for yesterday:
2017-11-16T18:00:00.000 AEDT
I trust you to do similarly for today.
Since you had AEDT in your requested result, I picked Australia/Sydney as time zone over Australia/Darwin or other Australian options.
Using the backport is also the futureproof solution: you will need the backport only until one day you upgrade to Java 8 or later (Java 9 is out already). When upgrading, simply change your import statement from org.threeten.bp to java.time.
Related
I'm stuck with the daylight problem in android the time should be +3 GMT/UTC
but I'm getting only +2. is there any solution or I can't handle it? please find below my code
TimeZone.getDefault().useDaylightTime();
SimpleDateFormat df = new SimpleDateFormat("HH:mm",Locale.ENGLISH); //"2021-02-18T11:00:00"
df.setTimeZone(TimeZone.getTimeZone("UTC"));
return df.format(calendar.getTime());
Note that the DST OFFSET is returning zero. what should I do to handle daylight?
Another note my time is Jordan / Amman,
In winter this should return +2 but in summer it should return +3
java.time
Consider using java.time, the modern Java date and time API, for your date and time work. Let’s first declare a formatter for your desired time format:
private static final DateTimeFormatter TIME_FORMATTER
= DateTimeFormatter.ofPattern("HH:mm", Locale.ENGLISH);
Now you may format the time from your Calendar in this way:
// Assume you are getting a Calendar from somewhere
Calendar calendar = new GregorianCalendar();
ZonedDateTime dateTime = calendar.toInstant().atZone(ZoneId.systemDefault());
String dateTimeString = dateTime.format(TIME_FORMATTER);
System.out.println(dateTimeString);
I ran the code just now, that is, 16:33 in UTC or 19:33 in Jordan. The output was:
19:33
If you don’t depend on getting on old-fashioned Calendar from somewhere, it’s probably even simpler and cleaner. For example, to get the current time in your time zone:
String timeString
= LocalTime.now(ZoneId.systemDefault()).format(TIME_FORMATTER);
System.out.println(timeString);
What went wrong in your code?
You set the time zone of your SimpleDateFormat to UTC. So the time in UTC was printed regardless of your default time zone. And since UTC hasn’t got summer time (DST), no such was taken into account.
BTW, this method call of yours does nothing:
TimeZone.getDefault().useDaylightTime();
From the documentation:
Returns:
true if this TimeZone uses Daylight Saving Time, false,
otherwise.
So the method does not alter anything and certainly not the UTC time zone. It only queries whether the mentioned time zone uses summer time (daylight saving time). So since the Asia/Amman time zone does, it should return true in your case.
Question: Doesn’t java.time require Android API level 26?
java.time works nicely on both older and newer Android devices. It just requires at least Java 6.
In Java 8 and later and on newer Android devices (from API level 26) the modern API comes built-in.
In non-Android Java 6 and 7 get the ThreeTen Backport, the backport of the modern classes (ThreeTen for JSR 310; see the links at the bottom).
On older Android either use desugaring or the Android edition of ThreeTen Backport. It’s called ThreeTenABP. In the latter case make sure you import the date and time classes from org.threeten.bp with subpackages.
Links
Oracle tutorial: Date Time explaining how to use java.time.
Java Specification Request (JSR) 310, where java.time was first described.
ThreeTen Backport project, the backport of java.time to Java 6 and 7 (ThreeTen for JSR-310).
Java 8+ APIs available through desugaring
ThreeTenABP, Android edition of ThreeTen Backport
Question: How to use ThreeTenABP in Android Project, with a very thorough explanation.
Could some one explain why this past date getting increased by one hour , when I convert it to Moscow Timezone ?
I'm using JDK 1.6.0_12 version. .
2011-04-02T11:39:46+0300 --> Sat Apr 02 12:39:46 MSK 2011 // 11:39 --> 12:39
My current system time-zone is "Europe/Moscow" UTC+3 .
Also please note that this past date is in DST(Daylight Saving ) time-zone period UTC+4 , earlier used in Russia.
There was a legislative change of Russian time-zone definitions in October 2014 . Since then Russia uses UTC+3 all through out a year .
I already checked
this old post of 2014 . But I think this issue looks different.
Our developers expect that every past date (like "2011-04-02T11:39:46+0300" and which is in DST period ), should contain current time zone offset value i.e +0300 , not +0400 . And they think JRE is converting it incorrectly to UTC+4 , though "Default Time Zone Offset" shows +3 here . Is this way of handling time-zone offset value for past dates correct?
Same output is given on JRE 1.8 , which I think is an updated version ,there shouldn't be any issue in TZ definition in JRE 1.8.
Thanks in Advance !
Java Code:
import java.text.SimpleDateFormat;
import java.util.TimeZone;
import java.util.Date;
public class HelloWorld{
public static void main(String []args)
{
String dateInString = "2011-04-02T11:39:46+0300";
System.out.println(dateInString);
try {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
Date date = dateFormat.parse(dateInString);
System.out.println(date);
} catch (Exception e) {
System.out.println(e);
}
final TimeZone tzone = TimeZone.getDefault();
System.out.println("Default Time Zone ID - " + tzone.getID());
System.out.println("Default Time Zone Offset - (" + (tzone.getRawOffset() / 60 / 60 / 1000) + ") hour.");
}
}
Output :
2011-04-02T11:39:46+0300
Sat Apr 02 12:39:46 MSK 2011
Default Time Zone ID - Europe/Moscow
Default Time Zone Offset - (3) hour.
12:39 is the correct time
You are getting the correct result. In your string, 2011-04-02T11:39:46+0300, the trailing +0300 is an offset from UTC. So the point in time is the same as 2011-04-02T08:39:46+00:00 (UTC). As you say yourself, Moscow was at UTC offset +04:00 from 27 March 2011 to 26 October 2014. So to get the correct time for Moscow Java needs to add 1 hour to the hour in the string. Or 4 hours to the UTC hour of 08:39:46. In any case the time in Moscow was 12:39:46 at this point in time.
Or to answer your question:
… why this past date getting increased by one hour , when I convert it
to Moscow Timezone ?
Because Moscow on that date was 1 hour ahead of the time in the string.
java.time
That said I agree with those who recommend java.time, the modern Java date and time API, for the job. SimpleDateFormat is a notorious troublemaker of a class, and Date and TimeZone are poorly and confusingly designed too. All are long outdated. The modern API is so much nicer to work with.
For example:
ZoneId zone = ZoneId.of("Europe/Moscow");
ZonedDateTime zdt = ZonedDateTime.of(2011, 4, 2, 11, 39, 46, 0, zone);
System.out.println(zdt);
Output:
2011-04-02T11:39:46+04:00[Europe/Moscow]
You can also see from the output that Java knows that Moscow was at offset +04:00 back then.
Your question very well illustrates why java.time (opposite the old TimeZone class) makes the distinction between a time zone and an offset. A time zone includes all historic, the present and all known future offsets from UTC. This is what you need to represent historic times in Moscow correctly. In java.time a time zone is identified by a ZoneId object and obeys a ZoneRules object (most often we need not concern ourselves with the latter and can just trust Java to make the right conversions). A UTC offset is represented by a ZoneOffset object.
Question: how could I use java.time with Java 1.6?
This is your lucky day. java.time exactly requires at least Java 6.
In Java 8 and later and on newer Android devices (from API level 26) the modern API comes built-in.
In non-Android Java 6 and 7 get the ThreeTen Backport, the backport of the modern classes (ThreeTen for JSR 310; see the links at the bottom).
On older Android either use desugaring or the Android edition of ThreeTen Backport. It’s called ThreeTenABP. In the latter case make sure you import the date and time classes from org.threeten.bp with subpackages.
Links
Time Changes in Moscow Over the Years
Oracle tutorial: Date Time explaining how to use java.time.
Java Specification Request (JSR) 310, where java.time was first described.
ThreeTen Backport project, the backport of java.time to Java 6 and 7 (ThreeTen for JSR-310).
Java 8+ APIs available through desugaring
ThreeTenABP, Android edition of ThreeTen Backport
Question: How to use ThreeTenABP in Android Project, with a very thorough explanation.
Both modern java date/time api and legacy one (that is used in jdk1.6) rely on system unix time and the tzdata file bundled with the JRE. Looks like the developers are right and your java is using a very old one version of tzdata and your developers are right.
Also, the tzdata keeps information about legal changes and if you are trying to convert date/time in the past, it will apply conversion rules that were relevant at that time.
Regarding JDK 1.8: there was an update to Russian timezone information in 8u101, so you should use at least 8u101 for a better timezone conversion.
The best decision for you would be to use modern java or update your JREs tzdata manually if you really need to use an old one.
You need to set time-zone to SimpleDateFormat as shown below:
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;
public class Main {
public static void main(String[] args) throws ParseException {
String dateInString = "2011-04-02T11:39:46+0300";
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
dateFormat.setTimeZone(TimeZone.getTimeZone("Europe/Moscow"));// Set time-zone
Date date = dateFormat.parse(dateInString);
System.out.println(dateFormat.format(date));
}
}
Output:
2011-04-02T12:39:46+0400
Note that java.util.Date does not have time-zone information. It's simply the number of milliseconds from the standard Java epoch of 1970-01-01T00:00:00Z where Z stands for UTC (0 hour offset), also known as Zulu time-zone. At any given moment, you will get the same number of milliseconds on the JVMs sitting in any part of the word. When you try to print an object of java.util.Date, the date-time string for the JVM's time-zone is calculated from this milliseconds value and the same is displayed. If you want to get the date-time String in a specific time-zone, you need to set it explicitly to the SimpleDateFormat and use the same to format the java.util.Date.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I have a client written for Android and a Server written in Java. The server stores information, for example:
[{daysEpoch: ... , amount: 1}, {daysEpoch: ... , amount: 34},{daysEpoch: ... , amount: 76}] etc....
where daysEpoch is days from 01/01/1970.
Every day at midnight (+ 5 seconds), the client asks for the sum of items from epochDays=x where x is today. The problem is that "today" on the client is one day after (for example) the server which is UTC.
Obviously the return value is wrong. My first thought was to send the client's millis to the server, and it works but I wonder if there's a more proper way to handle this.
A bit more info: The items/amounts are there from a long time ago, Every day at midnight, the client asks for the sum of "today". If the client is gmt+1 (for example), midnight on the mobile is 11pm yesterday on the server. Only at 1am it will be "today" on the server. I hope it's clearer now.
ISO 8601
I am presenting two suggestions. I am not sure which one will best fulfil your goal. Both suggestions involve ISO 8601, the international standard for dates and times.
1. Send date and time in UTC: Since it works to send a point in time that is independent of time zone, I suggest that you do that by sending the date and time in UTC. For example:
2020-03-22T19:04:57.110540Z
This is more human readable than a count of seconds or milliseconds since the epoch, yet perfectly machine readable too. The trailing Z means UTC.
2. Send just the date in UTC: The problem as I understood it was that the dates didn’t agree because of different time zones. However the client knows its own timezone and can fairly easily find the date in UTC and send it to the server, and then the server will understand.
2020-03-22
Again you may append Z to make it explicit that the date is in UTC if you prefer. It’s not much used, though occasionally. Again this format is readable by both humans and machines, which is why I’d prefer it over a count of days since the epoch day.
Edit: From the chat:
I want the data for "today" of the client.
And from the question:
The problem is that "today" on the client is one day after (for
example) the server which is UTC.
Then I don’t understand how that is a problem. As far as I can see, you were getting what you say that you want.
I would still recommend sending the date in ISO 8601 rather than a count of days since the epoch.
How to write and read ISO 8601 in Java?
java.time, the modern Java date and time API, has been designed to work excellently with ISO 8601. To write date and time in UTC from a device in an arbitrary time zone:
String dateTimeUtcString = Instant.now().toString();
System.out.println(dateTimeUtcString);
Example output from running just now:
2020-03-22T19:34:48.034Z
In case you were wondering why I presented six decimals on the seconds before and only print three decimals this time: the number of decimals is free within the standard, you may even leave out the decimal part of the seconds completely if you don’t need it. It will all work.
To read this string on the server, parse back into an Instant:
Instant i = Instant.parse(dateTimeUtcString);
If you wanted just the date, to write the UTC date from the device:
String dateString = LocalDate.now(ZoneOffset.UTC).toString();
System.out.println(dateString);
2020-03-22
Edit: since you want the client’s “today”, you should use ZoneId.systemDefault() instead of ZoneOffset.UTC in the call to LocalDate.now() above.
And to parse it on the server;
LocalDate date = LocalDate.parse(dateString);
If you wanted Z to indicate UTC, it’s slightly longer:
String dateUtcString = OffsetDateTime.now(ZoneOffset.UTC)
.format(DateTimeFormatter.ISO_OFFSET_DATE);
System.out.println(dateUtcString);
2020-03-22Z
To parse use the same formatter in the server:
LocalDate date = LocalDate.parse(
dateUtcString, DateTimeFormatter.ISO_OFFSET_DATE);
Question: Doesn’t java.time require Android API level 26?
java.time works nicely on both older and newer Android devices. It just requires at least Java 6.
In Java 8 and later and on newer Android devices (from API level 26) the modern API comes built-in.
In non-Android Java 6 and 7 get the ThreeTen Backport, the backport of the modern classes (ThreeTen for JSR 310; see the links at the bottom).
On (older) Android use the Android edition of ThreeTen Backport. It’s called ThreeTenABP. And make sure you import the date and time classes from org.threeten.bp with subpackages.
Links
Wikipedia article: ISO 8601
Oracle tutorial: Date Time explaining how to use java.time.
Java Specification Request (JSR) 310, where java.time was first described.
ThreeTen Backport project, the backport of java.time to Java 6 and 7 (ThreeTen for JSR-310).
ThreeTenABP, Android edition of ThreeTen Backport
Question: How to use ThreeTenABP in Android Project, with a very thorough explanation.
Im using SimpleDateFormat to get the current date and hour, the Date is working well but for unknown reasons the hour value is 3 hours backwards.
for example if I will run the code on 19:40 the time value will be 16:40 and I don't know why.
Would really appreciate some help.
String timeStamp = new SimpleDateFormat("M/d/yyyy&HH:mm").format(Calendar.getInstance().getTime());
java.time and ThreeTenABP
java.time, the modern Java date and time API, gives a pretty natural way to control the time zone. For example:
ZoneId zone = ZoneId.of("Asia/Istanbul");
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("M/d/uuuu&HH:mm");
String timeStamp = ZonedDateTime.now(zone).format(formatter );
System.out.println(timeStamp);
When I ran this snippet just now, the output was:
6/6/2019&19:53
I don’t know what your time zone is and trust you to specify the correct one. It matters.
Question: Can I use java.time on Android?
Yes, java.time works nicely on older and newer Android devices. It just requires at least Java 6.
In Java 8 and later and on newer Android devices (from API level 26) the modern API comes built-in.
In Java 6 and 7 get the ThreeTen Backport, the backport of the modern classes (ThreeTen for JSR 310; see the links at the bottom).
On (older) Android use the Android edition of ThreeTen Backport. It’s called ThreeTenABP. And make sure you import the date and time classes from org.threeten.bp with subpackages.
What went wrong in your code?
As others have said, your issue is a time zone issue. Apparently your SimpleDateFormat had its time zone set to UTC. When you’re not specifying time zone, it picks up its time zone from the JVM, which in turn usually picks it up from the operating system if not specified otherwise. That your JVM is using UTC is a standard practice, so not that much of a surprise.
Anyway, the datetime classes you were using, SimpleDateFormat and Calendar, are poorly designed, the former in particular notoriously troublesome. Fortunately both are long outdated. Instead use java.time, the modern Java date and time API.
I've just noticed that the phone hour is 3 hours backwards as well. Is
there a way to change it?
Most likely the phone system clock is correct (maybe even updated automatically from a time server), but its time zone is set to UTC. There should be a way to set it to your time zone in the phone settings (then it will likely also be used by your JVM).
ISO 8601
As an aside your timestamp format is peculiar. I recommend that you use a standard format, best ISO 8601, and also that you include offset in the string so that even if the time zone was wrong, the time would still be unambiguous and correct. Two examples. First you may use UTC consciously:
String timeStamp = Instant.now().toString();
2019-06-06T16:57:19.493599Z
The trailing Z means UTC.
If you want your own time zone:
String timeStamp = OffsetDateTime.now(zone).toString();
2019-06-06T19:58:29.788376+03:00
Links
Oracle tutorial: Date Time explaining how to use java.time.
Java Specification Request (JSR) 310, where java.time was first described.
ThreeTen Backport project, the backport of java.time to Java 6 and 7 (ThreeTen for JSR-310).
ThreeTenABP, Android edition of ThreeTen Backport
Question: How to use ThreeTenABP in Android Project, with a very thorough explanation.
Wikipedia article: ISO 8601
A Date does not have any timezone information; it's just a wrapper around a long that is assumed to be UTC epoch milliseconds. It's no coincidence that your timezone is 3 hours ahead of UTC and the formatted time is 3 hours out.
Although you can specify the timezone that SimpleDateFormat renders a Date in:
SimpleDateFormat format = new SimpleDateFormat("M/d/yyyy&HH:mm");
format.setTimeZone(TimeZone.getTimeZone("Europe/Moscow"));
Due to their many problems, Date and Calendar are largely deprecated.
Use LocalDateTime and DateTimeFormatter instead:
String timeStamp = LocalDateTime.now().format(DateTimeFormatter.ofPattern("M/d/yyyy&HH:mm"));
SimpleDateFormat from = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
SimpleDateFormat to = new SimpleDateFormat("MM/dd/yyyy Z");
Date date = from.parse("2017-08-10T00:00:00.000+0000");
String newDateStr = to.format(date);
The newDateStr is 08/09/2017 -0500. I expect that newDataStr can have the same timezone as the original date string(0000) -- 08/10/2017 -0000. What should I do to achieve this?
Preserving the offset from the original string is much easier with java.time, the modern Java date and time API:
DateTimeFormatter from = DateTimeFormatter.ofPattern("uuuu-MM-dd'T'HH:mm:ss.SSSZ");
DateTimeFormatter to = DateTimeFormatter.ofPattern("MM/dd/yyyy Z");
String newDateStr = OffsetDateTime.parse("2017-08-10T00:00:00.000+0000", from)
.format(to);
System.out.println(newDateStr);
This prints
08/10/2017 +0000
This is not the only reason for recommending changing to the modern API. The Date class that you are using is long outdated, and SimpleDateFormat in particular is renowned for being troublesome to work with. I always recommend staying away from those classes in 2018. The modern API is generally much nicer. And since you are already using Java 8, there is absolutely no reason why you shouldn’t want it (for anyone reading along and using Java 6 or 7, the new API has been backported to those Java versions too).
What went wrong?
An old-fashioned Date object is just a point in time, it doesn’t hold a time zone or offset in it. So after you had parsed your string into a Date, the offset information was no longer there. So neither the Date nor to, the new formatter, had any way of knowing which offset or time zone had been in the original string. Instead, the new formatter used its own time zone. Since you had not explicitly set a time zone on the formatter, its time zone was that of your JVM, which apparently was a time zone that was on offset -5 from UTC on these days of September 2017.
There are tricky ways to solve the problem with the outdated classes, but as I have probably said already, I wouldn’t want to bother.
"Call requires API Level 26" for Android
#Ponomarenko Oleh in a comment writes:
Pay attention: "Call requires API Level 26" for Android.
However, java.time works nicely on both older and newer Android devices. It just requires at least Java 6.
In Java 8 and later and on newer Android devices (from API level 26) the modern API comes built-in. This is also the intended meaning of the message quoted in the comment.
In non-Android Java 6 and 7 get the ThreeTen Backport, the backport of the modern classes (ThreeTen for JSR 310; see the links at the bottom).
On (older) Android use the Android edition of ThreeTen Backport. It’s called ThreeTenABP. And make sure you import the date and time classes from org.threeten.bp with subpackages.
Links
Oracle tutorial: Date Time explaining how to use java.time.
Java Specification Request (JSR) 310, where java.time was first described.
ThreeTen Backport project, the backport of java.time to Java 6 and 7 (ThreeTen for JSR-310).
ThreeTenABP, Android edition of ThreeTen Backport
Question: How to use ThreeTenABP in Android Project, with a very thorough explanation.
You could use the Java 8 Date API which provides a good set of functionality
to convert dates into strings (including timezone support).
An example:
ZonedDateTime dateTime = ZonedDateTime.parse("2017-08-10T00:00:00.000+0000", DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSZ"));
String newDateStr = dateTime.format(DateTimeFormatter.ofPattern("dd/MM/yyyy Z"));
For more information you can look at:
How to parse/format dates with LocalDateTime? (Java 8)
https://www.mscharhag.com/java/java-8-date-time-api