How to Convert String to LocalDateTime? - java

I have a string "2015-09-17T12:00". How can I convert this String to LocalDateTime in format "yyyy-MM-dd HH:mm" and then convert it back to String?

you can Replace T with whitespace(s):
String str="2015-09-17T12:00";
str.replace("T"," ");
afterwards convert to Date using SimpleDateFormat;

We can go with DateTimeFormatter, its thread safety.
Refer to below url:
https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html
sample program on converting String to Local Date Time
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd 'T' HH:mm");
LocalDateTime localDateTime = LocalDateTime.parse("2019-18-04 T 18:51", formatter);
Local Date time provides lot of method to get the hours or minutes or seconds
https://docs.oracle.com/javase/8/docs/api/java/time/LocalDateTime.html

Related

Convert the Specific string to date and check it is greater than current date

I have a string s="2020-04-07T13:43:49-05:00"
i have to check if its greater than current date and i tried using instant date
Instant timestamp = Instant.parse(string);
But did not work and i tried with LocalDate
LocalDate date = LocalDate.parse(string, format);
this is also not working, how to parse it and check
You should parse it into OffsetDateTime since date string has an offset
A date-time with an offset from UTC/Greenwich in the ISO-8601 calendar system, such as 2007-12-03T10:15:30+01:00.
String s="2020-04-07T13:43:49-05:00";
OffsetDateTime dateTime = OffsetDateTime.parse(s);
and then check weather it is greater than or not using isBefore or isAfter by converting into LocalDateTime
LocalDateTime.now().isBefore(dateTime.toLocalDateTime())
You can also compare OffsetDateTime directly using isBefore and isAfter
OffsetDateTime.now().isBefore(dateTime)
This works fine for me using Java (jshell) 13:
jshell> import java.time.Instant
jshell> Instant.parse("2020-04-07T13:43:49-05:00")
$2 ==> 2020-04-07T18:43:49Z
Use SimpleDateFormat for this. Take your string and format it to the date and then check for it.
Date date= new Date();
String targetDate="2020-04-07 13:43:49";
Date date2= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(targetDate);
Now you can just check the two date by
if(date.getTime()>date2.getTime())
If you want to do it opposite way you can do that too.
Date date= new Date();
String currentDate=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date)
now just check the two strings with if condition.

How to convert a string date "2019-04-21T12:08:35" to SimpleDateFormat, there by convert to Date?

For normal date Strings like "2019-04-08 08:35"
SimpleDateFormat df = new SimpleDateFormat("yyyy-dd-mm hh:mm");
but What shall be the shortest conversion for dates like
"2019-04-21T12:08:35"
SimpleDateFormat is not a Date, it's used to
Convert String to Date
Format Date to String
You can parse String to java.time.LocalDateTime directly since java8:
LocalDateTime localDateTime = LocalDateTime.parse("2019-04-21T12:08:35");
System.out.println(localDateTime);
Before Java8 :
SimpleDateFormat df = new SimpleDateFormat("yyyy-dd-yy'T'hh:mm:ss");

Java 8 DateTimeFormatter to ignore millisecond and zone

I am struggling with Java 8 DateTimeFormatter.
I would like to convert a given String to dateFormat and parse to LocalDateTime
Here is my code
DateTimeFormatter f = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss")
String text = "2020-01-01T01:01:11.123Z"
LocalDateTime date = LocalDateTime.parse(text, f)
But Java throws
Text could not be parsed, unparsed text found at index 19
If I change ofPattern to yyyy-MM-dd'T'HH:mm:ss.SSSX, my code executes without any error.
But I don’t want to use millisecond and time zone.
Do this instead:
String text = "2020-01-01T01:01:11.123Z";
LocalDateTime date = ZonedDateTime.parse(text)
.toLocalDateTime();
To get rid of the milliseconds information, do:
LocalDateTime date = ZonedDateTime.parse(text)
.truncatedTo(ChronoUnit.SECONDS)
.toLocalDateTime();
You can also use OffsetDateTime in place of ZonedDateTime.

how to print DateTime value

I have this code and I want to print out the time as String without the 'T' character between date and time.
String datetime4 =new StringBuilder().append(date4).append(time4).toString();
DateTime newdt=new DateTime(datetime4);
DateTimeFormatter formatter = DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ss");
newdt = formatter.parseDateTime(datetime4);
System.out.println(newdt);
Notice that date4 and time4 are String variables.
It will print:
2017-11-04T11:23:00.000+02:00
One way of doing it:
String date4 = "2017-02-02";
String time4 = "12:00:00";
//To parse it to Temporal object
DateTime dateTime = DateTime.parse(date4 +"T"+ time4);
// to output it as String in a prefered format (Thanks #Hugo)
System.out.println(dateTime.toString("yyyy-MM-dd HH:mm:ss"));
If you prefer Java 8 you will need to use formatter I think, LocalDateTime doesn't overload toString in the same way as JodaTime.
But not sure why you want to do this? seems like just appending both date and time is enough? Anyway if you want to parse to the date you need to put T as is needed to pass it as a valid date time format to DateTime as well as LocalDateTime if using Java8, then you can reformat it as you wish.
String date4 = "2017-02-02";
String time4 = "12:00:00";
LocalDateTime dateTime = LocalDateTime.parse(date4 +"T"+ time4);
System.out.println(dateTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
Using Java 8 LocalDateTime;
LocalDateTime dateTime;
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("uuuu-MM-dd'T'HH:mm:ss");
DateTimeFormatter desiredFormat = DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm:ss");
dateTime = LocalDateTime.parse("2017-06-01T12:10:10", formatter);
System.out.println(desiredFormat.format(dateTime));
When you do:
System.out.println(newdt);
You're printing the newdt variable, and internally println calls the toString() method on the object.
As this variable's type is DateTime, this code outputs the result of newdt.toString(). And Datetime.toString() method uses a default format that contains the "T".
If you want the output String to have a different format, you can do something like this:
System.out.println(newdt.toString("yyyy-MM-dd HH:mm:ss"));
The output will be:
2017-11-04 11:23:00
(without the "T")
You can use this version of toString with any pattern accepted by DateTimeFormatter.
You can also create another DateTimeFormatter for the format you want:
DateTimeFormatter withoutT = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss");
System.out.println(withoutT.print(newdt));
The output will be the same, it's up to you to choose.

Easiest way to parse date in String format to GregorianCalendar

I have the following date:
2011-10-07T08:51:52.006Z
Now I want to parse it into a GregorianCalendar. Is there an easier way to do it than using substrings and parsing them to Integers?
And what is the Z in the time string?
I tried to parse it using SimpleDateFormat, but I can´t find a explanation for the T in the date String.
DateFormat format = new SimpleDateFormat( "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'" )
Date date = format.parse( "2011-10-07T08:51:52.006Z" );
Calendar calendar = new GregorianCalendar();
calendar.setTime( date );
I would take a look at DateTimeFormatter
DateTimeFormatter formatter =
DateTimeFormat.forPattern("<custom_pattern>").withOffsetParsed();
DateTime dateTime = formatter.parseDateTime("<your_input>");
GregorianCalendar cal = dateTime.toGregorianCalendar();
The T in your string acts as a separator between the date and the time and the Z is the time-zone information both as per ISO-8601 format.
You could use the SimpleDateFormatter to parse the String. Please read the javadoc for the aforementioned class to know what could be the format string. 'Z' indicates the timezone information.

Categories

Resources