Instant.now equivalent to java.util.Date or Java 7 - java

I have a system using Java 7 and I need to generate a date equivalente to Instant.now (Java 8).
For example, Instant.now().toString() generate a date like that:
"2018-12-19T12:32:46.816Z"
Using java.util.Date I have this date: "2018-12-19T10:38:13.892"
Date date = new Date(System.currentTimeMillis());
SimpleDateFormat sdf;
sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS");
String text = sdf.format(date);
System.out.println(text);
I don't know if I can simply concatenate a "Z" at the end of this date.
Without "Z" another system that parse this date using Instant.parse throws the error:
java.time.format.DateTimeParseException: Text
'2018-12-19T10:38:13.892' could not be parsed at index 23
at java.base/java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.java:1988)
at java.base/java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1890)
at java.base/java.time.Instant.parse(Instant.java:395)

Z means UTC time zone, you can set the time zone to UTC and append Z mannually:
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
sdf.setTimeZone(TimeZone.getTimeZone("UTC"));

ThreeTen Backport
java.time, the modern Java date and time API, has been backported. So just do as you would in Java 8:
String text = Instant.now().toString();
Running just now I got:
2018-12-19T13:37:37.186Z
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 new 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.
The outdated SimpleDateFormat
If you don’t want to rely on an external dependency just until you move to Java 8 or higher, the (most) correct solution is this combination of pieces from the two other answers (one of them now deleted):
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXX");
sdf.setTimeZone(TimeZone.getTimeZone("Etc/UTC"));
String text = sdf.format(System.currentTimeMillis());
2018-12-19T13:37:37.285Z
It doesn’t always give the exact same string as Instant.toString, but it will give a string that Instant can parse in the other system. While Instant.toString() only prints as many decimals as necessary, the above SimpleDateFormat will print three decimals also when some of them are 0.
When the time zone of the formatter is UTC, format pattern letter (uppercase) X will print Z as “time zone” (really just an offset).
Links
ThreeTen Backport project, the backport of java.time to Java 6 and 7 (ThreeTen for JSR-310).
Java Specification Request (JSR) 310, where java.time was first described.
ThreeTenABP, Android edition of ThreeTen Backport
Question: How to use ThreeTenABP in Android Project, with a very thorough explanation.
Oracle tutorial: Date Time explaining how to use java.time.

Related

2021-04-05T16:25:45.000+00:00 Time stamp change in SimpleDateFormat("yyyy-MM-dd hh:mm:ss a")

My Code
String date = "2021-04-05T16:25:45.000+00:00";
SimpleDateFormat inputFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
SimpleDateFormat outputFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss a");
Date parsedDate = null;
try {
parsedDate = inputFormat.parse(date);
} catch (ParseException e) {
e.printStackTrace();
}
String formattedDate = outputFormat.format(parsedDate);
Error:
java.lang.NullPointerException: Attempt to invoke virtual method 'long java.util.Date.getTime()' on a null object reference
java.time through desugaring
Consider using java.time, the modern Java date and time API, for your date and time work. Use for example this output formatter:
private static final DateTimeFormatter OUTPUT_FORMATTER
= DateTimeFormatter.ofPattern("yyyy-MM-dd hh:mm:ss a", Locale.forLanguageTag("en-IN"));
With it do:
String date = "2021-04-05T16:25:45.000+00:00";
OffsetDateTime dateTime = OffsetDateTime.parse(date);
String formattedDate = dateTime.format(OUTPUT_FORMATTER);
System.out.println(formattedDate);
Output:
2021-04-05 04:25:45 PM
You may use a different locale for the output formatter as appropriate for your requirements. The choice of locale determines which strings are used for AM and PM.
I am exploiting the fact that the string that you have got is in ISO 8601 format. offsetDateTime parses this format as its default, that is, without any explicit formatter.
What went wrong in your code?
You tried using a format pattern string of yyyy-MM-dd'T'HH:mm:ss.SSS'Z' for a date string of 2021-04-05T16:25:45.000+00:00. The 'Z' in single quotes in the format pattern string means that your date string must end in a literal Z. When instead it ended in +00:00, parsing failed with a ParseException. If you didn’t see the output from e.printStackTrace(); in your code, you have got a serious flaw in your project setup that you should fix before worrying about how to parse the date string. In any case, since parsing failed, parsedDate kept its initial value of null, which caused outputFormat.format(parsedDate) to throw the NullPointerException that you did see.
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.
Wikipedia article: ISO 8601

Parse UTC time format into readable datetime in android

I have an UTC datetime in the format 2019-12-06T06:04:50.022461Z I want to convert into readable datetime in the format dd-mm-yyyy hh:mm:ss. I am not able to convert in the particular format. Any help will be highly appreciated. Thanks
java.time and ThreeTenABP
I am assuming that you want to display the time in your user’s time zone. As you said already, your string is in UTC, and very few users will be happy to read the time in UTC.
DateTimeFormatter readableFormatter = DateTimeFormatter.ofPattern("dd-MM-uuuu HH:mm:ss");
String originalString = "2019-12-06T06:04:50.022461Z";
String readableString = Instant.parse(originalString)
.atZone(ZoneId.systemDefault())
.format(readableFormatter);
System.out.println(readableString);
For the sake of the example I ran this code in America/Sitka time zone. Output was:
05-12-2019 21:04:50
If you did want to display the time in UTC as in the original string:
String readableString = OffsetDateTime.parse(originalString)
.format(readableFormatter);
06-12-2019 06:04:50
Don’t go with the classes SimpleDateFormat, TimeZone and Calendar used in the currently accepted answer. They are all poorly designed, SimpleDateFormat in particular is a notorious troublemaker and also cannot parse 6 decimals on the seconds as in your string. Those three classes are also all long outdated. Instead I am using java.time, the modern Java date and time API. It is so much nicer to work with. The code is not only shorter, I think that you will also find it clearer to read (either immediately or when you get used to the fluent style).
I am exploiting the fact that your original string is in ISO 8601 format. The classes of java.time parse ISO 8601 format as their default, that is, without any explicit formatter.
For the vast majority of purposes you shold not want to convert from one string format to another, though. Inside your program keep your date and time as an Instant or other proper date/time object. only convert to a string when a string is needed in the interface.
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
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
You can use SimpleDateFormat to format the date. Check below:
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
Calendar calendar = Calendar.getInstance();
try {
calendar.setTime(sdf.parse("2019-12-06T06:04:50.022461Z"));
} catch (Exception ex) {
ex.printStackTrace();
}
SimpleDateFormat returnFormat = new SimpleDateFormat("dd-mm-yyyy hh:mm:ss");
returnFormat.format(calendar.getTime());
You can try this in java 8
String dateString = "2019-12-06T06:04:50.022461Z";
DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd").withZone(ZoneOffset.UTC);
TemporalAccessor date = fmt.parse(dateString);
Instant time = Instant.from(date);
DateTimeFormatter fmtOut = DateTimeFormatter.ofPattern("dd-mm-yyyy hh:mm:ss").withZone(ZoneOffset.UTC);
String newDateString = fmtOut.format(time);

format sql datetime in java android studio

How can I format this DateTime form SQL 2019-02-18T06:30:38.9933333 to 2/18/19, 6:30 AM in the android studio using java
I already try Simpledatetime nothing happens.`
String startDate="2019-02-18T06:30:38.9933333"; // Input String
SimpleDateFormat sdf1 = new SimpleDateFormat("MM-dd-yyyy h:mm tt");
java.util.Date date = sdf1.parse(startDate);
java.sql.Date sqlStartDate = new java.sql.Date(date.getTime());
need help.. thannk you.
java.time, ThreeTenABP and a built-in localized format
DateTimeFormatter formatter
= DateTimeFormatter.ofLocalizedDateTime(FormatStyle.SHORT)
.withLocale(Locale.US);
String startDate = "2019-02-18T06:30:38.9933333"; // Input String
LocalDateTime dateTime = LocalDateTime.parse(startDate);
System.out.println(dateTime.format(formatter));
Output:
2/18/19 6:30 AM
Still better than the above, get a LocalDateTime from your database, or if this is not possible yet, then a Timestamp that you convert like this:
LocalDateTime dateTime = DateTimeUtils.toLocalDateTime(timestampFromDatabase);
Or if using Java 8 or later:
LocalDateTime dateTime = timestampFromDatabase.toLocalDateTime();
The formatting is as before.
As a rule Java knows the best format for a particular audience better than you do. Using the built-in format also saves you from the error-prone task of writing a format pattern string.
In the first snippet I am exploiting the fact that your input string conforms with ISO 8601, the international standard format that the java.time classes parse (and also print) as their default, that is, without any explicit formatter.
The classes SimpleDateFormat, java.util.Date and java.sql.Date are all poorly designed and long outdated. Instead I use java.time, the modern Java date and time API.
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.
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
Maybe you are use java.time.LocalDateTime if you are working with java8 or higher:
String startDate = "2019-02-18T06:30:38.9933333"; // Input String
LocalDateTime localDateTime = LocalDateTime.parse(startDate);
Use format:
yyyy-MM-dd'T'HH:mm:ss.SSSSSSS
For more Reference:
https://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html

Convert WallClock Time from one timezone to another timezone

I want to convert wall clock time time from one TZ to another without doing OFFSET math myself.
SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss z");
String d = sdf.format(new Date());
System.out.println(d);
sdf.setTimeZone(TimeZone.getTimeZone("America/New_York"));
String d1 = sdf.format(new Date());
System.out.println(d1);
Output:
2018.07.09 13:43:30 PDT
2018.07.09 16:43:30 EDT
Desired Output
2018.07.09 13:43:30 PDT
2018.07.09 13:43:30 EDT
How can I get the desired output?
java.time and ThreeTen Backport
DateTimeFormatter formatter
= DateTimeFormatter.ofPattern("yyyy.MM.dd HH:mm:ss z", Locale.US);
ZonedDateTime zdt = ZonedDateTime.now(ZoneId.of("America/Los_Angeles"));
String d = zdt.format(formatter);
System.out.println(d);
zdt = zdt.withZoneSameLocal(ZoneId.of("America/New_York"));
String d1 = zdt.format(formatter);
System.out.println(d1);
Output when I ran the code just now:
2018.07.10 04:30:20 PDT
2018.07.10 04:30:20 EDT
The ZonedDateTime class that you mentioned in a comment has your desired conversion built in, in its withZoneSameLocal method. This returns the same wall clock time in the specified time zone.
As of now, we use Java 7. We have not upgraded our infra to java 8…
No big problem. java.time and its ZonedDateTime work nicely on Java 7. They just require at least Java 6.
In Java 8 and later and on newer Android devices (from API level 26, I’m told) the modern API comes built-in.
In Java 6 and 7 get the ThreeTen Backport, the backport of the new 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.timeto 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.

String Date + Time to Date Object Java

I am trying to convert a string "8/2/2018 04:25 AM" into a date object but it seems to be converting it wrong making it output a completely wrong date and time when I do a date.toString(). Note - I do not care about the date.toString() format, I just need it to be the same date and time
Here is my code,
String timestampString = dateText.getText().toString() + " " + timeText.getText().toString();
try {
Date timestamp2 = new SimpleDateFormat("dd/MM/yyyy hh:mm a").parse(timestampString);
Log.d("TIME", timestamp2.toString());
} catch (ParseException e) {Log.d("TAG", e)}
Here is the output:
D/TIME: Wed Feb 07 23:25:00 EST 2018
if anyone could lead me in the right direction, it is greatly appreciated.
DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("d/M/uuuu");
DateTimeFormatter timeFormatter = DateTimeFormatter.ofPattern("hh:mm a", Locale.ENGLISH);
LocalDate date = LocalDate.parse(dateText.getText(), dateFormatter);
LocalTime time = LocalTime.parse(timeText.getText(), timeFormatter);
LocalDateTime dateTime = date.atTime(time);
Log.d("TIME", dateTime.toString());
Prints:
D/TIME: 2018-02-08T04:25
I am using and recommending java.time, the modern Java date and time API.The Date class that you were using is long outdated, SimpleDateFormat is too and also notoriously troublesome. Avoid those if you can. java.time is so much nicer to work with. And offers the LocalXx classes that don’t have time zones, which guarantees to guard you against time zone issues. That said, you may want to convert your date-time into a ZonedDateTime in a time zone of your choice to make it an unambiguous point in time.
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, I’m told) the modern API comes built-in.
In Java 6 and 7 get the ThreeTen Backport, the backport of the new 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.timeto 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.

Categories