0

I have to convert the date from API in the 24-hour format but SimpleDateFormat("dd/MM/yy kk:mm") is showing 24:30 instead of 00:30.

I am receiving the date in this format: "2020-05-25 10:34:27"

I am converting the date as follows:

      val newDate: Date =  SimpleDateFormat("yyyy-MM-dd hh:mm:ss", 
    Locale.getDefault()).parse(vehiculos[b].fecha)
    val d = SimpleDateFormat("yyyy-MM-dd kk:mm", Locale.getDefault())
            .parse(SimpleDateFormat("yyyy-MM-dd HH:mm:ss", 
    Locale.getDefault()).format(newDate))
    val newFecha = SimpleDateFormat("dd/MM/yy kk:mm", Locale.getDefault()).format(d)
Sweta Jain
  • 3,248
  • 6
  • 30
  • 50
  • Using the uppercase letter K will solve the problem. – Hossein Sep 09 '21 at 14:10
  • As an aside consider throwing away the long outmoded and notoriously troublesome `SimpleDateFormat` and friends. See if you either can use [desugaring](https://developer.android.com/studio/write/java8-support-table) or add [ThreeTenABP](https://github.com/JakeWharton/ThreeTenABP) to your Android project, in order to use java.time, the modern Java date and time API. It is so much nicer to work with. – Ole V.V. Sep 09 '21 at 15:13

2 Answers2

1

You should use the uppercase letter K

SimpleDateFormat("dd/MM/yy KK:mm", Locale.getDefault())

k: Hour in day (1-24)

K: Hour in am/pm (0-11)

Check this site to test it out: http://www.sdfonlinetester.info/

D.Kastier
  • 2,640
  • 3
  • 25
  • 40
1

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

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161