1

I am receiving a json payload which has the time in minutes, I need to convert it to HH:mm and may even require to set the time zone also,

I tried this solution

  SimpleDateFormat sdf = new SimpleDateFormat("mm");
 sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
    
    try {
        Date dt = sdf.parse("90");
        sdf = new SimpleDateFormat("HH:mm");
        System.out.println(sdf.format(dt));
    } catch (ParseException e) {
        e.printStackTrace();
    }

So there any simplified way to do this

Thejas
  • 379
  • 5
  • 24
  • 3
    Please have a look at the "new" DateTime API (the classes in the [`java.time`](https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/time/package-summary.html) package). `SimpleDateFormat` and `java.util.Date` should no longer be used in new code. Specifically, the [`DateTimeFormatter`](https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/time/format/DateTimeFormatter.html) will be interesting for you. – Hulk Jul 19 '21 at 14:59
  • However, if we are talking about a duration and not a time, then [this answer](https://stackoverflow.com/a/41800301/2513200) shows several ways to do this, probably the best way would be to use `Duration` in that case. – Hulk Jul 19 '21 at 15:08

1 Answers1

4

java.time

The java.util Date-Time API and their formatting API, SimpleDateFormat are outdated and error-prone. It is recommended to stop using them completely and switch to the modern Date-Time API*.

Solution using java.time, the modern Date-Time API:

You can use java.time.Duration which is modelled on ISO-8601 standards and was introduced with Java-8 as part of JSR-310 implementation. With Java-9 some more convenient methods were introduced.

Demo:

import java.time.Duration;

public class Main {
    public static void main(String[] args) {
        int minutes = 123;
        Duration duration = Duration.ofMinutes(minutes);

        // Default format
        System.out.println(duration);

        // Custom format
        // ####################################Java-8####################################
        String formattedDuration = String.format("%d:%02d", duration.toHours(), duration.toMinutes() % 60);
        System.out.println(formattedDuration);
        // ##############################################################################

        // ####################################Java-9####################################
        formattedDuration = String.format("%d:%02d", duration.toHoursPart(), duration.toMinutesPart(),
                duration.toSecondsPart());
        System.out.println(formattedDuration);
        // ##############################################################################
    }
}

Output:

PT2H3M
2:03
2:03

ONLINE DEMO

Learn more about the modern Date-Time API from Trail: Date Time.

When you say 123 minutes, it is independent of timezone i.e. duration of 123 minutes will have the same value in any timezone. Check Duration is not the same as Date-Time to understand the difference between a Date-Time and a Duration. In layman's terms, a Date-Time is like a vector quantity while Duration is like a scalar quantity.


* For any reason, if you have to stick to Java 6 or Java 7, you can use ThreeTen-Backport which backports most of the java.time functionality to Java 6 & 7. If you are working for an Android project and your Android API level is still not compliant with Java-8, check Java 8+ APIs available through desugaring and How to use ThreeTenABP in Android Project.

Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110
  • thank you for the detailed explanation,I wanted to change the time zone also – Thejas Jul 19 '21 at 15:47
  • 1
    @Thejas - When you say 123 minutes, it is independent of timezone i.e. a duration of 123 minutes will have the same value in any timezone. Check [Duration is not the same as Date-Time](https://stackoverflow.com/a/68397531/10819573) to understand the difference between a Date-Time and a Duration. In layman's terms, a Date-Time is like a vector quantity while Duration is like a scalar quantity. – Arvind Kumar Avinash Jul 19 '21 at 16:41