I am trying to convert a date String from the server to dateTime with ThreeTenBP. My method looks like this:
String toDateTime(String dateString) {
DateTimeFormatter formatter = new DateTimeFormatterBuilder().appendPattern("M/d/yyyy h:mm a").toFormatter();
ZonedDateTime dateTimeWithZone = ZonedDateTime.parse(dateString, formatter);
return dateTimeWithZone.toString();
}
However, I get an exception:
DateTimeParseException: Text '2015-07-21T09:26:06.260-05:00' could not
be parsed at index 4
What am I doing wrong?
Your pattern begins with "M/" which would indicate the month. Your actual date string begins with "2015-" which is obviously the year. Actually that date looks like it's in ISO 8601 format and a pattern like "yyyy-MM-dd'T'HH:mm:ss.SSSX" would match.
Related
I have a problem parsing a String to LocalDate.
According to similar questions on Stackoverflow and documentation I am using the correct values dd (day of the month), MM (month of the year) and yyyy (year).
My String
String mydate = "18.10.2022 07:50:18";
My parsing test code
System.out.println(
LocalDate.parse(testPasswordExp)
.format(DateTimeFormatter.ofPattern("dd.MM.yyyy HH:mm:ss")
)
);
Error:
Caused by: java.lang.RuntimeException:
java.time.format.DateTimeParseException:
Text '18.10.2022 07:50:18' could not be parsed at index 0
The main problem of your code example is that you first parse the String to a LocalDate without the use of a suitable DateTimeFormatter and then format() it with a DateTimeFormatter that tries to format hour of day, minute of hour and second of minute which just aren't there in a LocalDate.
You can parse this String to a LocalDate directly, but better parse it to a LocalDateTime because your String contains more than just information about
day of month
month of year
year
Your myDate (and probably the testPasswordExp, too) has a time of day. You can get a LocalDate as the final result that way, too, because a LocalDateTime can be narrowed down toLocalDate().
A possible way:
public static void main(String[] args) {
// example datetime
String testPasswordExp = "18.10.2022 07:50:18";
System.out.println(
LocalDateTime // use a LocalDateTime and…
.parse( // … parse …
testPasswordExp, // … the datetime using a specific formatter,
DateTimeFormatter.ofPattern("dd.MM.uuuu HH:mm:ss")
).toLocalDate() // then extract the LocalDate
);
}
Output:
2022-10-18
You don't use the specified format for parsing, you use it to format the parsed date.
LocalDate.parse(mydate)
… uses the default ISO_LOCAL_DATE format. You are looking for this overload:
LocalDate.parse(mydate, DateTimeFormatter.ofPattern("dd.MM.yyyy HH:mm:ss"))
This method uses the specified format for parsing string to date. See this code run at Ideone.com.
Note that you are using LocalDate, meaning it will throw away the time part, keeping only the date after parsing. You probably meant to use LocalDateTime.
You can use
String mydate = "18.10.2022 07:50:18";
LocalDate ld = LocalDate.parse(mydate, DateTimeFormatter.ofPattern("dd.MM.yyyy HH:mm:ss"));
System.out.println(ld.toString());
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.
I'm trying to get the current DateTime with my DateTimeFormat pattern, but i'm getting the exception...
//sets the current date
DateTime currentDate = new DateTime();
DateTimeFormatter dtf = DateTimeFormat.forPattern("dd/MM/YYYY HH:mm").withLocale(locale);
DateTime now = dtf.parseDateTime(currentDate.toString());
I'm getting this exception, I cannot understand who is giving the malformed format
java.lang.IllegalArgumentException: Invalid format: "2017-01-04T14:24:17.674+01:00" is malformed at "17-01-04T14:24:17.674+01:00"
This line DateTime now = dtf.parseDateTime(currentDate.toString()); isn't correct because you try parse date with default toSring format. You have to parse string which formatted the same way as pattern:
DateTime currentDate = new DateTime();
DateTimeFormatter dtf = DateTimeFormat.forPattern("dd/MM/YYYY HH:mm").withLocale(locale);
String formatedDate = dtf.print(currentDate);
System.out.println(formatedDate);
DateTime now = dtf.parseDateTime(formatedDate);
System.out.println(now);
You are using the wrong format to parse the date. If you print out the date you are trying to parse after converting it to a String with toString you get:
2017-01-04T14:24:17.674+01:00
This date string does not conform to the pattern dd/MM/YYYY HH:mm. To parse the to a string converted currentDate to a DateTime object again, you have to use the following pattern:
DateTimeFormatter dtf = DateTimeFormat.forPattern("YYYY-MM-dd'T'HH:mm:ss.SSSZ")
.withLocale(locale);
Parsing with this DateTimeFormatter will get you another instance that represents the same time as the original currentDate.
For more details on the DateTimeFormatter and it's parsing options check out the JavaDoc
So I am trying to convert a string into an iso format for the date.
This is the string that I am trying to convert "2016-07-05 02:14:35.0"
I would like to have it in this format the iso 8601
"2016-07-05T02:14:35.0"
I have this so far
DateTimeFormatter format = DateTimeFormat.forPattern("YYYY-MM-DDTHH:mm:sszzz");
new LocalDate();
LocalDate newDate = LocalDate.parse(created,format);
created = newDate.toString();
But it is giving me this exception
ERROR: Illegal pattern component: T; nested exception is java.lang.IllegalArgumentException: Illegal pattern component: T
I followed the examples and I don't know what I am doing wrong here.
Any help would be appreciated.
Firstly, that value is a LocalDateTime, not a LocalDate. If you want to get a date out in the end, I'd convert it to a LocalDateTime first, then take the date part of that.
When performing date formatting and parsing, always read the documentation really carefully. It looks like you're using Joda Time (due to using forPattern; if you can move to Java 8 that would be beneficial). That means you should be reading the DateTimeFormat docs.
Current problems with your pattern:
You're using 'D' instead of 'd'; that means day-of-year
You've specified 'T' without quoting it, and it isn't in the pattern anyway
You've ignored the fraction-of-second part of your value
You've specified 'zz' when there's no time zone indicator in the value.
Here's a working example:
import org.joda.time.*;
import org.joda.time.format.*;
public class Test {
public static void main(String[] args) {
String text = "2016-07-05 02:14:35.0";
DateTimeFormatter format = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss.S");
LocalDateTime localDateTime = LocalDateTime.parse(text, format);
System.out.println(localDateTime);
}
}
If you actually want to parse values with T in the middle, you'd use a pattern of "yyyy-MM-dd'T'HH:mm:ss.S" - note how then the T is quoted so it's treated literally instead of as a format specifier.
Note that this is just parsing. It's not "converting a string into ISO date format" - it's converting a string into a LocalDateTime. If you then want to format that value in an ISO format, you need to be using DateTimeFormatter.print, with an appropriate format. For example, you might want to convert to a format of yyyy-MM-dd'T'HH:mm:ss.S':
import org.joda.time.*;
import org.joda.time.format.*;
public class Test {
public static void main(String[] args) {
String text = "2016-07-05 02:14:35.0";
DateTimeFormatter parser = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss.S");
LocalDateTime localDateTime = LocalDateTime.parse(text, parser);
DateTimeFormatter printer = DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ss.S");
String formatted = printer.print(localDateTime);
System.out.println(formatted); // Output 2016-07-05T02:14:35.0
}
}
The code above will only handle a single digit fraction-of-second. You could parse using .SSS instead of .S, but you really need to work out what you want the output to be in different cases (e.g. for 100 milliseconds, do you want .1 or .100?).
You have some errors in your code:
The pattern should be 'yyyy-MM-dd HH:mm:ss.SSS'. Be aware of upper-
and lowercase.
Use LocalDateTime to get date and time. LocalDate only holds the date.
The corrected code:
String created = "2016-07-05 02:14:35.000";
DateTimeFormatter format = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS");
LocalDateTime newDate = LocalDateTime.parse(created,format);
created = newDate.toString();
System.out.println(created);
Use the following format to convert
String format = "yyyy-mm-dd hh:mm:ss"
You are using the wrong format to convert. Using T is only to separate the date from time.
Use the format like this
String = "yyyy-MM-dd'T'HH:mm:ss"
I'm using Joda time DateTimeFormatter to create a new datetime object in yyyy-mm-dd string format. When calling DateTime date = formatter.parse(String) I get a DateTime object with an extra hour. I live in UTC +1. How to format a string datetime without hours added.
String date = "2013-01-28";
DateTimeFormatter formatter = DateTimeFormat.forPattern("yyyy-mm-dd");
DateTime date = formatter.parseDateTime(date);
date.ToString() = 2013-01-28T00:01:00.000+01:00
expected = 2013-01-28T00:00:00.000+01:00
Additionally, later in the code I compare two DateTime objects. This parser is in yymmdd format and it does not add one hour.
String date = "130102"
DateTimeFormatter format = DateTimeFormat.forPattern("yyMMdd");
DateTime datetime = format.parseDateTime(date);
datetime.toString = 2013-01-02T00:00:00.000+01:00
yyyy-mm-dd uses the minute-of-hour, not the month (pattern symbol M). Please refer to the documentation of pattern symbols on Joda-Time-page.
Parsing "2013-01-28" with your wrong pattern yields "2013-01-28T00:01:00.000+01:00". Do you see the minute equal to 1? And the month of January seems to be a default value if the parser cannot find a month information (in my opinion not smart).