Home > front end >  How to convert time string into date and time format?
How to convert time string into date and time format?

Time:09-17

I having a hard time trying to format a string time into MM:DD::YY and only time. From an IP i getting the time in the following format

2021-09-10T00:37:42Z

and I'm want to display the date and time in:

09/08/2021

Time

09:50PM

CodePudding user response:

Parse the given string into OffsetDateTime and then get LocalDate and LocalTime parts out of it.

import java.time.LocalDate;
import java.time.LocalTime;
import java.time.OffsetDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Locale;

public class Main {
    public static void main(String[] args) {
        String strDateTime = "2021-09-10T00:37:42Z";
        OffsetDateTime odt = OffsetDateTime.parse(strDateTime);
        LocalTime time = odt.toLocalTime();
        LocalDate date = odt.toLocalDate();
        System.out.println(time);
        System.out.println(date);

        // #########Custom formats #########

        DateTimeFormatter dtfDate = DateTimeFormatter.ofPattern("MM/dd/uuuu", Locale.ENGLISH);
        String formattedDateString = date.format(dtfDate);
        System.out.println(formattedDateString);

        DateTimeFormatter dtfTime = DateTimeFormatter.ofPattern("hh:mm a", Locale.ENGLISH);
        String formattedTimeString = time.format(dtfTime);
        System.out.println(formattedTimeString);
    }
}

Output:

00:37:42
2021-09-10
09/10/2021
12:37 AM

ONLINE DEMO

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

Update based on an important comment by Ole V.V.:

The OP — or their user — may want the date and time in their own time zone.

In order to get the date and time parts in a specific timezone e.g. America/Los_Angeles, you should parse the given date-time string into ZonedDateTime and convert the same to the ZonedDateTime of the specific timezone using ZonedDateTime#withZoneSameInstant. Rest of the things will remain same as the original answer.

Demo:

import java.time.LocalDate;
import java.time.LocalTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Locale;

public class Main {
    public static void main(String[] args) {
        String strDateTime = "2021-09-10T00:37:42Z";
        ZonedDateTime zdt = ZonedDateTime.parse(strDateTime);

        ZonedDateTime zdtLosAngeles = zdt.withZoneSameInstant(ZoneId.of("America/Los_Angeles"));

        LocalTime time = zdtLosAngeles.toLocalTime();
        LocalDate date = zdtLosAngeles.toLocalDate();
        System.out.println(time);
        System.out.println(date);

        // #########Custom formats #########

        DateTimeFormatter dtfDate = DateTimeFormatter.ofPattern("MM/dd/uuuu", Locale.ENGLISH);
        String formattedDateString = date.format(dtfDate);
        System.out.println(formattedDateString);

        DateTimeFormatter dtfTime = DateTimeFormatter.ofPattern("hh:mm a", Locale.ENGLISH);
        String formattedTimeString = time.format(dtfTime);
        System.out.println(formattedTimeString);
    }
}

Output:

17:37:42
2021-09-09
09/09/2021
05:37 PM

ONLINE DEMO


* 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.

  • Related