Convert String to Date | LocalDate | LocalTime | LocalDateTime | Instant

Java Date conversion

Date Class is present in both the java.util and java.sql packages. The formatting and parsing classes are defined in the java.text package.

Here we are converting string value to java.util.Date class.

Convert String to date

The Date class is used to store a time instant without timezone information and has many problems, which are addressed in Java 8 Date API.

SimpleDateFormat class is used to format and parse string values into Date class


String input = "14-06-1997";
DateFormat dateFormat = new SimpleDateFormat("dd-mm-yyyy", Locale.ENGLISH);
Date date = dateFormat.parse(input);

Java 8 Date API Conversion

Now we will see how to convert string value to the Java 8 date and time API.

  • If the input value format contains only date data, then call LocalDate.parse() method for converting LocalDate class
  • If the input value format contains only the time part, use localTime.parse().
  • If the input value format consists of both data and time, use the localDateTime.parse() method.
  • If the input value contains a zone, then use ZonedDateTime.parse().

Convert String to localdate

LocalDate is an immutable class that represents Date with default format of "yyyy-MM-dd". By using now(), we can retrieve the current date. We can also provide input arguments for year, month and date to create a LocalDateb instance.


DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("dd-MM-yyyy");
LocalDate date2 = LocalDate.parse(input, dateTimeFormatter);
System.out.println(date2);
        

Convert String to localTime

LocalTime is an immutable class whose instance represents a time in the human readab le format. It’s default format is hh:mm:ss.zzz. Similarly to LocalDate, this class provides time zone support and allows instance creation by passing hour, minute    , and second as input arguments.


String time = "12:15:11";
DateTimeFormatter dateTimeFormatter1 = DateTimeFormatter.ofPattern("HH:mm:ss");
LocalTime localTime = LocalTime.parse(time, dateTimeFormatter1);
System.out.println(localTime);
        

Convert String to localdateTime

The LocalDateTime class represents dates with the default format "yyyy-MM-dd-HH-mm-ss.zzz". It provides a factory method that takes LocalDate and LocalTime input arguments to create a LocalDateTime instance.


String dateTime = "14-06-1997:12:15:11";
DateTimeFormatter dateTimeFormatter2 = DateTimeFormatter.ofPattern("dd-MM-yyyy:HH:mm:ss");
LocalDateTime dateTime2 = LocalDateTime.parse(dateTime, dateTimeFormatter2);    
System.out.println(dateTime2);
        

We've seen that when we supply invalid arguments to create Date/Time, java.time.DateTimeException is thrown. This is a RuntimeException so we don't need to catch it explicitly.

The parse() method in SimpleDateFormat will throw a ParseException if it cannot parse the input string because our input does not match our format pattern.

Convert String to Instant

An instant class is used to work with machine-readable time formats. Instant stores date time as a Unix timestamp.


String input = "14-06-1997";
DateFormat dateFormat = new SimpleDateFormat("dd-mm-yyyy", Locale.ENGLISH);
Instant instant = dateFormat.parse(input).toInstant();
System.out.println(instant);

Convert date to milliseconds

We can get milliseconds value from Date class using getTime() method.

If you have string value then first we need to convert string value to Date class and use getTime() method to get milliseconds.


String input = "14-06-1997";
DateFormat dateFormat = new SimpleDateFormat("dd-mm-yyyy", Locale.ENGLISH);
Date date= dateFormat.parse(input);
long milliSeconds=date.getTime();

If you have a Calendar object, we can use the getTimeInMillis() method.


Calendar calendar = Calendar.getInstance();
calendar.getTimeInMillis();

If you have an Instant class, we can use the toEpochMilli() method.


Instant instant = Instant.now();
instant.toEpochMilli();
        

Post a Comment

Previous Post Next Post

Recent Posts

Facebook