Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
I used DateTimeFormatter.ofPattern('yyyy-MM-dd'T'HH:mm:ssZ')
and I get error like this
java.time.format.DateTimeParseException: Text '2020-04-13T12:05:54+0600' could not be parsed at index 19
String which i wanna parse is '2020-04-13T12:05:54+0600'
so how can i solve this? What pattern i need to use?
Your code is using LocalDate which only parses a date - not a date and time so you are getting an error when the parse finds the space after the date.
So you should be using LocalDateTime but LocalDateTime.parse(String) expects an ISO format date which is not the format you are using.
So you need to use a DateTimeFormatter to specify the format of your input string. Something like:
DateTimeFormatter format = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSSX");
LocalDateTime result = LocalDateTime.parse(convertDate, format);
Answer copied from greg449’s answer here
You can pass pattern of Like this way.
DateTimeFormatter.ofPattern("yyyy MM dd");
Please follow the documentation to pass proper pattern.
https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 11 months ago.
Improve this question
I'm trying to parse an String into a java.util.Date.
Currently, I'm using SimpleDateFormat, with the "yyyy-MM-dd'T'HH:mm:ss.SSSXXX" format String, and it works pretty well most of the time; for example, those work okay:
"2022-03-16T12:09:56.267Z"
"2022-03-16T12:11:55.017+03:00"
The problem lies with perfectly valid ISO strings that happen to use less than three digits for the miliseconds:
"2022-03-16T09:18:31.9Z"
It throws this exception: java.text.ParseException: Unparseable date: "2022-03-16T09:18:31.9Z".
Is there a way to handle those? Please, do keep in mind that I need to return a java.util.Date, but using SimpleDateFormat is optional.
I'm using Java 8.
Here is one way.
Note the Z stands for Zulu.
And also remember that Date does not store any time zone information.
If necessary, you can modify the ZonedDateTime instance before converting to Date.
Instant d = Instant.parse("2022-03-16T09:18:31.9Z");
Date date = Date.from(d);
System.out.println(d);
System.out.println(date);
prints
2022-03-16T09:18:31.900Z
Wed Mar 16 05:18:31 EDT 2022
I would recommend that you try to convert to using the classes in the java.time package as they are quite superior to Date.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
I have a String value as follows
2018-12-05 18:11:27.187
How can I can convert it into a LocalDate object based on that format "MM/dd/YYYY HH:mm:ss" ?
a LocalDate object is what it is. It has no format; it is an object with methods; these methods make it do stuff.
You can for example ask a localdate to return the year of the date it represents. You can also ask it to render itself as a string using some format. That string is then not a LocalDate (it is a String).
Furthermore, a localdate represents a date. Hence the name. 'hour' is not part of a date. YYYY is the pattern for week based year. You don't want that.
So, fixing your misconceptions, we end up with:
DateTimeFormatter inFormat = DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm:ss.SSS");
DateTimeFormatter outFormat = DateTimeFormatter.ofPattern("MM/dd/uuuu HH:mm:ss");
LocalDateTime when = LocalDateTime.parse("2018-12-05 18:11:27.187", inFormat);
System.out.println(outFormat.format(when));
Firstly you need to define the pattern of your current Date and Time input
Parse the current Date and Time to LocalDateTime class
Print the value to the new Date and Time format you want.
LocalDateTime date = LocalDateTime.parse("2018-12-05 18:11:27.187", DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm:ss.SSS"));
System.out.println(date.format(DateTimeFormatter.ofPattern("MM/dd/uuuu HH:mm:ss")));
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
Trying to convert the account Expiry date in the format 8/18/2019 12:0:0 AM SGT to dd/MM/yyyy format but getting format exceptions in Java
Trying to convert the account Expiry date in the format 8/18/2019 12:0:0 AM SGT to dd/MM/yyyy format but getting format exceptions in Java
You might be getting an exception because the month in 8/18/2019 is 18 as per the format dd/MM/yyyy.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I'm a bit baffled what format these timestamps are in. I was told the format to use is yyyy-MM-dd.HH:mm:ss but all of the timestamps appear like this 2017-01-01 00:08:57.231, 2017-01-01 07:43:36.348, or 2017-01-01 13:25:55.683. I'm not understanding why there are four sections to the time ?:Hour:Minute:Second in the actual data I have when the format I'm supposed to be using only has three time sections. Are these datetime timestamps not actually in the format of yyyy-MM-dd.HH:mm:ss?
No, your suspicion is correct, your example date-time strings are not in the format yyyy-MM-dd.HH:mm:ss. The dot between dd and HH must be a simple mistake, it should be a space since there is a space between date and time in the timestamp strings. Furthermore all of your example strings include milliseconds: in 00:08:57.231 you’ve got 57 seconds and 231 milliseconds, or if you like, 57.231 seconds, so the last section may also be referred to as fraction of second.
DateTimeFormatter formatter
= DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm:ss.SSS");
String timestampString = "2017-01-01 00:08:57.231";
LocalDateTime dateTime = LocalDateTime.parse(timestampString, formatter);
System.out.println(dateTime);
Output:
2017-01-01T00:08:57.231
For the nerdily curious: It is possible to parse the string, or more precisely most of it, with the format you had been given, only correcting the dot into a space:
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
String timestampString = "2017-01-01 00:08:57.231";
LocalDateTime dateTime = LocalDateTime.from(
formatter.parse(timestampString, new ParsePosition(0)));
In this case the result comes without the milliseconds:
2017-01-01T00:08:57
I see no reason why you should want this, though.
Avoid SimpleDateFormat
In a comment you gave a snippet that uses the SimpleDateFormat class. This class is not only long outdated, it is also notoriously troublesome. I see no reason why you should want to use it. Instead I am using java.time, the modern Java date and time API. In my experience it is so much nicer to work with.
Links
Oracle tutorial: Date Time explaining how to use java.time. You may especially want to study the section Parsing and Formatting.
These time stamps are in yyyy-MM-dd HH:mm:ss:ms format, last three digits are milliseconds.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I am trying to build a program that libraries can use. The user inputs the date they loaned the book, and it calculates that date + 5 days (when the book is due). Then checks if the current date is before or after the due date. Then says if it is late or not.
Use SimpleDateFormat to take the user input (as a String), and
convert it to a Date. Use Calendar for adding days to a given Date.
See also:
SimpleDateFormat
Date
Calendar
Use JodaTime instead and then you can call plusDays() on a DateTime object. There is also an isBefore() method as well. Here is a simple example:
DateTime loanDate = new DateTime();
DateTime dueDate = new DateTime().plusDays(8);
DateTime date = loanDate.plusDays(5);
System.out.println(date.isBefore(dueDate));
This returns true as the date is before the due date.