java.time
Consider using java.time, the modern Java date and time API, for your date and time work. Using java.time keep your date and time in a LocalDateTime
object. A LocalDateTime
is a date and time of day without time zone of offset from UTC. You could also well consider ZonedDateTime
instead, a date and time with time zone.
When you receive the string from the API, parse it. I am showing how to do it using this formatter:
private static final DateTimeFormatter API_PARSER
= new DateTimeFormatterBuilder().append(DateTimeFormatter.ISO_LOCAL_DATE)
.appendLiteral(' ')
.append(DateTimeFormatter.ISO_LOCAL_TIME)
.toFormatter(Locale.ROOT);
Parsing goes like the following. For demonstration I have taken a time of day with 00
hours so that we can check whether they come out as 24
after formatting.
String fromApi = "2020-05-25 00:34:27";
LocalDateTime dateTime = LocalDateTime.parse(fromApi, API_PARSER);
System.out.println(dateTime);
Output from this snippet is:
2020-05-25T00:34:27
Only when you need to give string output, for example to the user, format the LocalDateTime
we got into a string. We need a second formatter for that:
private static final DateTimeFormatter NEW_FORMATTER
= DateTimeFormatter.ofPattern("dd/MM/yy HH:mm", Locale.ROOT);
With it we can do:
String formattedDateTime = dateTime.format(NEW_FORMATTER);
System.out.println(formattedDateTime);
25/05/20 00:34
We got 00
hours as desired.
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.
What did you do wrong?
- For most purposes trying to convert a date and time from one string format to another is the wrong approach. Don’t keep your dates and times as strings. Keep proper date-time objects. For example
ZonedDateTime
or LocalDateTime
.
- You were using the poorly designed and long outdated date and time classes
Date
and SimpleDateFormat
.
- You were over-complicating things. You were convertng from
String
to Date
, again to String
, again to Date
and one last time to String
. There’s no way you need all of that, and each operation is one more opportunity for making an error.
- As others have already said,
kk
is for clock-hour-of-day (1-24), which explains why you didn’t get 00
but 24
in your last conversion. In java.time upper case HH
is for hour of day from 00
through 23
.
- Not taking time zone into account may or may not be an error. If you know that the server and the user are in the same time zone, or the server knows the user’s time zone and hands out the date in accordance, you’re fine. If the server and the user use different time zones, a time zone conversion of your date and time is called for.
Links