Trying To Create A Timestamp "mm/dd/yyyy Hh:mm A" For A Chat For Android Using Java
Solution 1:
java.time and ThreeTenABP
There are more than one unclear point about your question, but I am trying a suggestion. For my way of doing it I need two formatters:
privatestaticDateTimeFormattertimestampFormatter=newDateTimeFormatterBuilder()
.appendValue(ChronoField.INSTANT_SECONDS)
.appendValue(ChronoField.MILLI_OF_SECOND, 3)
.toFormatter();
privatestaticDateTimeFormatterdtf= DateTimeFormatter.ofPattern("MM/dd/yyyy hh:mm a");
I understand that chatList.get(i).getTimestamp()
may return null
, but also that in this case you haven’t got any chat message to display. We need to take this possibility into account. The code below will give you the string NA
. Modify if you want something else. Next it would seem from your commented-out code (the code that crashed on the null
) that it may also return a String
containing a long count of milliseconds since the epoch. So I am handling that situation too.
int i = 0;
String timeStamp = chatList.get(i).getTimestamp();
String dateTime;
if (timeStamp == null) {
dateTime = "NA";
} else {
Instant inst = timestampFormatter.parse(timeStamp, Instant.FROM);
dateTime = inst.atZone(ZoneId.systemDefault()).format(dtf);
}
System.out.println(dateTime);
Example output in my time zone (Europe/Copenhagen) when chatList.get(i).getTimestamp()
returned 1567890123456
:
09/07/2019 11:02 PM
What went wrong in your code?
First case, when timeStamp
is null
and you try Long.parseLong(timeStamp)
, you will get a java.lang.NumberFormatException: null
. If your code doesn’t catch this exception, it crashes.
Second case, you were calling the toString
method of your SimpleDateFormat
. SimpleDateFormat
doesn’t define a toString
method itself so Object.toStrng()
is called. This returns such a string as java.text.SimpleDateFormat@4f47c0fb
. The class name and a funny hex value separated by an @
sign.
However, you should probably not want to use SimpleDateFormat
at all. That class is a notorious troublemaker and long outdated. Instead in my code I am using DateTimeFormatter
and other classes from java.time, the modern Java date and time API.
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 this case you need to substitute the constant
Instant.FROM
in the above code with the method referenceInstant::from
. - 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.
Solution 2:
You have to use toLocalizedPattern() in place of toString() in
StringdateTime=newSimpleDateFormat("mm/dd/yyyy hh:mm a", Locale.getDefault()).toString();
i.e you have to use it like this
StringdateTime=newSimpleDateFormat("mm/dd/yyyy hh:mm a", Locale.getDefault()).toLocalizedPattern();
Post a Comment for "Trying To Create A Timestamp "mm/dd/yyyy Hh:mm A" For A Chat For Android Using Java"