How to parse/format dates with LocalDateTime? (Java 8)

0 votes

I have date and time as string (e.g. "2014-04-08 12:30"). How can I obtain a LocalDateTime instance from the given string?

After I finished working with the LocalDateTime object: How can I then convert the LocalDateTime instance back to a string with the same format as shown above?

Oct 9, 2018 in Java by parth295
• 4,590 points

reshown Mar 18 by parth295 39 views

2 answers to this question.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
0 votes

To create a LocalDateTime object from a string you can use the static LocalDateTime.parse() method. It takes a string and a DateTimeFormatter as parameter. The DateTimeFormatter is used to specify the date/time pattern.

String str = "1986-04-08 12:30";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");
LocalDateTime dateTime = LocalDateTime.parse(str, formatter);

Formatting date and time

To create a formatted string out a LocalDateTime object you can use the format() method.

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");
LocalDateTime dateTime = LocalDateTime.of(1986, Month.APRIL, 8, 12, 30);
String formattedDateTime = dateTime.format(formatter); // "1986-04-08 12:30"

Note that there are some commonly used date/time formats predefined as constants in DateTimeFormatter. For example: Using DateTimeFormatter.ISO_DATE_TIME to format the LocalDateTime instance from above would result in the string "1986-04-08T12:30:00".

The parse() and format() methods are available for all date/time related objects (e.g. LocalDate or ZonedDateTime)

answered Oct 9, 2018 by sharth
• 3,310 points
0 votes
Converting LocalDateTime to Time Zone ISO8601 String
LocalDateTime d1 =; 
ZonedDateTime d2 = d1.atZone(ZoneOffset.UTC); //you might use a different zone
String iso8601 = d2.toString();

Converting from ISO8601 String back to a LocalDateTime
String iso8601 = "2016-02-14T18:32:04.150Z";
ZonedDateTime d3 = ZonedDateTime.parse(iso8601);
LocalDateTime d4 = d3.toLocalDateTime();
answered Dec 6, 2018 by Sushmita
• 6,820 points

Related Questions In Java

+5 votes
3 answers

How to execute a python file with few arguments in java?

You can use Java Runtime.exec() to run python script, ...READ MORE

answered Mar 27, 2018 in Java by DragonLord999
• 8,360 points

edited Nov 6, 2018 by Omkar 4,562 views
0 votes
3 answers

How to parse JSON in Java

import org.json.*; JSONObject obj = new JSONObject(" .... ...READ MORE

answered Aug 20, 2018 in Java by Daisy
• 8,020 points
0 votes
1 answer

How to pad an integer with zeros on the left in Java?

Use java.lang.String.format() method. String.format("%05d", number) ...READ MORE

answered May 31, 2018 in Java by Daisy
• 8,020 points
0 votes
2 answers

How to left pad a string with zero in java?

String paddedString = org.apache.commons.lang.StringUtils.leftPad("129018", 10, "0") the second ...READ MORE

answered Aug 31, 2018 in Java by Sushmita
• 6,820 points
0 votes
3 answers

Increment the date in Java by 1-day

import java.time.LocalDate; public class DateIncrementer { static ...READ MORE

answered Aug 1, 2018 in Java by Akrati
• 3,150 points
0 votes
1 answer

How to calculate the difference between two date instances in Java?

You can use Joda Time Library. Interval i ...READ MORE

answered May 4, 2018 in Java by parth295
• 4,590 points
0 votes
1 answer

Fetch list of in-between dates using Java

I tried generating the list of the ...READ MORE

answered May 10, 2018 in Java by geek.erkami
• 2,320 points
0 votes
1 answer

Convert Milliseconds to “X mins, x seconds” in Java?

I wrote a function which converts milliseconds ...READ MORE

answered May 30, 2018 in Java by Rishabh
• 3,520 points
0 votes
1 answer

How can I convert a String variable to a primitive int in Java

You can convert a String to int by using a function: int ...READ MORE

answered Apr 12, 2018 in Java by sharth
• 3,310 points
0 votes
3 answers

How to reverse a string in java?

public static String reverse(String s) { ...READ MORE

answered Aug 16, 2018 in Java by samarth295
• 2,190 points

© 2018 Brain4ce Education Solutions Pvt. Ltd. All rights Reserved.
"PMP®","PMI®", "PMI-ACP®" and "PMBOK®" are registered marks of the Project Management Institute, Inc. MongoDB®, Mongo and the leaf logo are the registered trademarks of MongoDB, Inc.