w3resource

Java: Convert String to date and time and vice a versa

Java DateTime, Calendar: Exercise-39 with Solution

Write a Java program to convert String to date and time and vice versa.

Sample Solution:

Java Code:

//MIT License: https://bit.ly/35gZLa3

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.OffsetDateTime;
import java.time.OffsetTime;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.Locale;

public class Main {

    public static void main(String[] args) throws ParseException {

        System.out.println("\nConvert String to LocalDate:");
        LocalDate localDate = LocalDate.parse("2020-06-01");
        System.out.println("Convert LocalDate to String:");
        String localDateAsDefaultString = localDate.toString();
        System.out.println("LocalDate: " + localDateAsDefaultString + "( year: " + localDate.getYear()
                + ", month: " + localDate.getMonthValue() + ", day: " + localDate.getDayOfMonth() + " )");

        LocalTime localTime = LocalTime.parse("12:23:44");
    	System.out.println("\nConvert LocalTime to String:");
        String localTimeAsDefaultString = localTime.toString();
        System.out.println("LocalTime: " + localTimeAsDefaultString + "( hour: " + localTime.getHour()
                + ", minute: " + localTime.getMinute() + ", second: " + localTime.getSecond() + " )");

        System.out.println("Convert String to LocalDateTime:");
        LocalDateTime localDateTime = LocalDateTime.parse("2020-06-01T11:20:15");
        System.out.println("Convert LocalDateTime to String:");
        String localDateTimeAsDefaultString = localDateTime.toString();
        System.out.println("LocalDateTime: " + localDateTimeAsDefaultString + "( year: " + localDateTime.getYear()
                + ", month: " + localDateTime.getMonthValue() + ", day: " + localDateTime.getDayOfMonth()
                + ", hour: " + localDateTime.getHour() + ", minute: " + localDateTime.getMinute()
                + ", second: " + localDateTime.getSecond() + " )");

        System.out.println("Convert String to OffsetDateTime:");
        OffsetDateTime offsetDateTime = OffsetDateTime.parse("2007-12-03T10:15:30+01:00");
        System.out.println("Convert OffsetDateTime to String:");
        String offsetDateTimeAsDefaultString = offsetDateTime.toString();
        System.out.println("OffsetDateTime: " + offsetDateTimeAsDefaultString + "( year: " + offsetDateTime.getYear()
                + ", month: " + offsetDateTime.getMonthValue() + ", day: " + offsetDateTime.getDayOfMonth()
                + ", hour: " + offsetDateTime.getHour() + ", minute: " + offsetDateTime.getMinute()
                + ", second: " + offsetDateTime.getSecond() + ", offset: " + offsetDateTime.getOffset() + " )");

        System.out.println("Convert String to OffsetTime:");
        OffsetTime offsetTime = OffsetTime.parse("10:15:30+01:00");
        System.out.println("Convert OffsetTime to String:");
        String offsetTimeAsDefaultString = offsetTime.toString();
        System.out.println("OffsetTime: " + offsetTimeAsDefaultString
                + "( hour: " + offsetTime.getHour() + ", minute: " + offsetTime.getMinute()
                + ", second: " + offsetTime.getSecond() + ", offset: " + offsetTime.getOffset() + " )");

        System.out.println("Convert String to ZonedDateTime:");
        ZonedDateTime zonedDateTime = ZonedDateTime.parse("2020-06-01T10:15:30+09:00[Asia/Tokyo]");
        System.out.println("Convert ZonedDateTime to String:");
        String zonedDateTimeAsDefaultString = zonedDateTime.toString();
        System.out.println("ZonedDateTime: " + zonedDateTimeAsDefaultString + "( year: " + zonedDateTime.getYear()
                + ", month: " + zonedDateTime.getMonthValue() + ", day: " + zonedDateTime.getDayOfMonth()
                + ", hour: " + zonedDateTime.getHour() + ", minute: " + zonedDateTime.getMinute()
                + ", second: " + zonedDateTime.getSecond() + ", offset: " + zonedDateTime.getOffset()
                + ", zone: " + zonedDateTime.getZone() + " )");

        System.out.println("\nJava 8, convert with formatter:");

        DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("dd.MM.yyyy");
        System.out.println("Convert String to LocalDate:");
        LocalDate localDateFormatted = LocalDate.parse("01.06.2020", dateFormatter);
        System.out.println("Convert LocalDate to String:");              
        String localDateAsFormattedString = dateFormatter.format(localDateFormatted);
        System.out.println("Date: " + localDateAsFormattedString + "( year: " + localDateFormatted.getYear()
                + ", month: " + localDateFormatted.getMonthValue() + ", day: " + localDateFormatted.getDayOfMonth() + " )");

        DateTimeFormatter timeFormatter = DateTimeFormatter.ofPattern("HH|mm|ss");
        System.out.println("Convert String to LocalTime:");
        LocalTime localTimeFormatted = LocalTime.parse("12|23|44", timeFormatter);
        System.out.println("Convert LocalTime to String:");              
        String localTimeAsFormattedString = timeFormatter.format(localTimeFormatted);
        System.out.println("Time: " + localTimeAsFormattedString + "( hour: " + localTimeFormatted.getHour()
                + ", minute: " + localTimeFormatted.getMinute() + ", second: " + localTimeFormatted.getSecond() + " )");

        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("dd.MM.yyyy, HH:mm:ss");
        System.out.println("Convert String to LocalDateTime:");
        LocalDateTime localDateTimeFormatted = LocalDateTime.parse("01.06.2020, 11:20:15", dateTimeFormatter);
        System.out.println("Convert LocalDateTime to String:");        
        String localDateTimeAsFormattedString = dateTimeFormatter.format(localDateTimeFormatted);
        System.out.println("DateTime: " + localDateTimeAsFormattedString + "( year: " + localDateTimeFormatted.getYear()
                + ", month: " + localDateTimeFormatted.getMonthValue() + ", day: " + localDateTimeFormatted.getDayOfMonth()
                + ", hour: " + localDateTimeFormatted.getHour() + ", minute: " + localDateTimeFormatted.getMinute()
                + ", second: " + localDateTimeFormatted.getSecond() + " )");

        DateTimeFormatter offsetDateTimeFormatter = DateTimeFormatter.ofPattern("yyyy.MM.dd, HH:mm:ss, XXXXX");
        System.out.println("Convert String to OffsetDateTime:");
        OffsetDateTime offsetDateTimeFormatted = OffsetDateTime.parse("2007.12.03, 10:15:30, +01:00", offsetDateTimeFormatter);
        System.out.println("Convert OffsetDateTime to String:");        
        String offsetDateTimeAsFormattedString = offsetDateTimeFormatter.format(offsetDateTimeFormatted);
        System.out.println("OffsetDateTime: " + offsetDateTimeAsFormattedString + "( year: " + offsetDateTimeFormatted.getYear()
                + ", month: " + offsetDateTimeFormatted.getMonthValue() + ", day: " + offsetDateTimeFormatted.getDayOfMonth()
                + ", hour: " + offsetDateTimeFormatted.getHour() + ", minute: " + offsetDateTimeFormatted.getMinute()
                + ", second: " + offsetDateTimeFormatted.getSecond() + ", offset: " + offsetDateTimeFormatted.getOffset() + " )");

        DateTimeFormatter offsetTimeFormatter = DateTimeFormatter.ofPattern("HH mm ss XXXXX");
        System.out.println("Convert String to OffsetTime:");
        OffsetTime offsetTimeFormatted = OffsetTime.parse("10 15 30 +01:00", offsetTimeFormatter);
        System.out.println("Convert OffsetTime to String:");
        String offsetTimeAsFormattedString = offsetTimeFormatter.format(offsetTimeFormatted);
        System.out.println("OffsetTime: " + offsetTimeAsFormattedString
                + "( hour: " + offsetTimeFormatted.getHour() + ", minute: " + offsetTimeFormatted.getMinute()
                + ", second: " + offsetTimeFormatted.getSecond() + ", offset: " + offsetTimeFormatted.getOffset() + " )");

        DateTimeFormatter zonedDateTimeFormatter
                = DateTimeFormatter.ofPattern("dd.MM.yyyy, HH:mm:ssXXXXX '['VV']'");
        // DateTimeFormatter zonedDateTimeFormatter 
        //      = DateTimeFormatter.ofPattern("dd.MM.yyyy, HH:mm:ssXXXXX '['VV']'").withZone(ZoneId.of("Europe/Paris"));        
        System.out.println("Convert String to ZonedDateTime:");                 
        ZonedDateTime zonedDateTimeFormatted
                = ZonedDateTime.parse("01.06.2020, 11:20:15+09:00 [Asia/Tokyo]", zonedDateTimeFormatter);
        System.out.println("Convert ZonedDateTime to String:");
        String zonedDateTimeAsFormattedString = zonedDateTimeFormatted.format(zonedDateTimeFormatter);
        System.out.println("ZonedDateTime: " + zonedDateTimeAsFormattedString + "( year: " + zonedDateTimeFormatted.getYear()
                + ", month: " + zonedDateTimeFormatted.getMonthValue() + ", day: " + zonedDateTimeFormatted.getDayOfMonth()
                + ", hour: " + zonedDateTimeFormatted.getHour() + ", minute: " + zonedDateTimeFormatted.getMinute()
                + ", second: " + zonedDateTimeFormatted.getSecond() + ", offset: " + zonedDateTimeFormatted.getOffset()
                + ", zone: " + zonedDateTimeFormatted.getZone() + " )");
    }
}

Sample Output:

Convert String to LocalDate:
Convert LocalDate to String:
LocalDate: 2020-06-01( year: 2020, month: 6, day: 1 )

Convert LocalTime to String:
LocalTime: 12:23:44( hour: 12, minute: 23, second: 44 )
Convert String to LocalDateTime:
Convert LocalDateTime to String:
LocalDateTime: 2020-06-01T11:20:15( year: 2020, month: 6, day: 1, hour: 11, minute: 20, second: 15 )
Convert String to OffsetDateTime:
Convert OffsetDateTime to String:
OffsetDateTime: 2007-12-03T10:15:30+01:00( year: 2007, month: 12, day: 3, hour: 10, minute: 15, second: 30, offset: +01:00 )
Convert String to OffsetTime:
Convert OffsetTime to String:
OffsetTime: 10:15:30+01:00( hour: 10, minute: 15, second: 30, offset: +01:00 )
Convert String to ZonedDateTime:
Convert ZonedDateTime to String:
ZonedDateTime: 2020-06-01T10:15:30+09:00[Asia/Tokyo]( year: 2020, month: 6, day: 1, hour: 10, minute: 15, second: 30, offset: +09:00, zone: Asia/Tokyo )

Java 8, convert with formatter:
Convert String to LocalDate:
Convert LocalDate to String:
Date: 01.06.2020( year: 2020, month: 6, day: 1 )
Convert String to LocalTime:
Convert LocalTime to String:
Time: 12|23|44( hour: 12, minute: 23, second: 44 )
Convert String to LocalDateTime:
Convert LocalDateTime to String:
DateTime: 01.06.2020, 11:20:15( year: 2020, month: 6, day: 1, hour: 11, minute: 20, second: 15 )
Convert String to OffsetDateTime:
Convert OffsetDateTime to String:
OffsetDateTime: 2007.12.03, 10:15:30, +01:00( year: 2007, month: 12, day: 3, hour: 10, minute: 15, second: 30, offset: +01:00 )
Convert String to OffsetTime:
Convert OffsetTime to String:
OffsetTime: 10 15 30 +01:00( hour: 10, minute: 15, second: 30, offset: +01:00 )
Convert String to ZonedDateTime:
Convert ZonedDateTime to String:
ZonedDateTime: 01.06.2020, 11:20:15+09:00 [Asia/Tokyo]( year: 2020, month: 6, day: 1, hour: 11, minute: 20, second: 15, offset: +09:00, zone: Asia/Tokyo )

Flowchart:

Flowchart: Java DateTime, Calendar Exercises - Get seconds since 1970

Java Code Editor:

Improve this sample solution and post your code through Disqus

Previous: Write a Java program to calculate the difference between two dates in days.
Next: Write a Java program to display current date without time and current time without date.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://w3resource.com/java-exercises/datetime/java-datetime-exercise-39.php