For questions specific to the `DateTimeFormatter` class of java.time or the like-named class of Joda-Time. May be combined with the [java-time] or the [jodatime] tag.
Questions tagged [datetimeformatter]
166 questions
33
votes
6 answers
java.util.date to String using DateTimeFormatter
How can I convert a java.util.Date to String using
DateTimeFormatter dateTimeFormatter = DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ss")
The Date object which I get is passed
DateTime now = new DateTime(date);

Cork Kochi
- 1,783
- 6
- 29
- 45
13
votes
3 answers
How can I make a leading zero in DateTime-Pattern optional
I have a user input field and would like to parse his date, whatever he puts in.
The user might provide his date with a leading zero or without one, so I wanna be able to parse an input like this
02.05.2019
and also this
2.5.2019
But as far as I…

John Du
- 247
- 1
- 2
- 7
12
votes
2 answers
What is the equivalent format string of DateTimeFormatter.ISO_OFFSET_DATE_TIME?
Do we know if there is an equivalent format string that outputs the same result as DateTimeFormatter.ISO_OFFSET_DATE_TIME?
i.e.
ZonedDateTime dateTime =…

user1589188
- 5,316
- 17
- 67
- 130
11
votes
3 answers
Parsing a date using DateTimeFormatter ofPattern
I'm trying to parse a string containing a date and time using the java.time.format.DateTimeFormatter (my ultimate goal is to get the date from this string into a java.time.LocalDate).
I keep getting DateTimeParseExceptions when trying to parse the…

Chris
- 839
- 2
- 10
- 33
8
votes
1 answer
Java 8 DateTimeFormatter dropping millis when they're zero?
This seems weird. Java 8 is formatting the output differently depending on whether the millis is zero. How do you force Java 8 (1.8.0_20) to always spit out the millis regardless of if they're zero or not?
public static void main(String[] args) {
…

Chris Kessel
- 5,583
- 4
- 36
- 55
7
votes
1 answer
DateTimeFormatter.parse returns an invalid Japanese date
I'm trying to convert a Japanese date string to JapaneseDate.
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("Gyy年MM月dd日")
.withChronology(JapaneseChronology.INSTANCE)
.withResolverStyle(ResolverStyle.LENIENT);
JapaneseDate d =…
user17223316
7
votes
3 answers
How to format LocalDate to ISO 8601 with T and Z?
I'm trying to generate a random date and time, and convert it to the "yyyy-MM-dd'T'HH:mm:ss'Z'" format.
Here is what I have tried:
public static String generateRandomDateAndTimeInString() {
LocalDate date =…

Jeff
- 7,767
- 28
- 85
- 138
6
votes
4 answers
Are time zone designators- "T" and "Z" case sensitive in ISO8601 format?
I am using following ISO8601 format:
YYYY-MM-DDThh:mm:ssZ
And I used OffsetDateTime.parse() to parse this format. I was able to parse date-time by passing t (instead of T) and z (instead of Z) here.
So can anyone tell if it is allowed in ISO8601 or…

Aishwer Sharma
- 205
- 2
- 17
6
votes
2 answers
DateTimeFormatter trouble with a pattern
I am writing a stock program that (so far) gets the data from "Markit on Demand" through a request such as this:
http://dev.markitondemand.com/Api/v2/Quote/xml?symbol=aapl
This returns the data in xml, with various measures of the stock (Symbol,…

Will Pike
- 281
- 4
- 17
5
votes
0 answers
Bug or Feature? Java 16 DateTimeFormatter issue with parsing localized month name
I'm using the en_GB locale, but a similar issue may also affect other en_XX locales.
Under Java 15 the following code works:
LocalDate.parse("10-Sep-17", DateTimeFormatter.ofPattern("dd-MMM-yy", Locale.UK));
Under Java 16 it gives:…

Tim Barrett
- 101
- 1
- 5
4
votes
1 answer
ZonedDateTime correctly parses "GMT" zone from pattern 'O' but not "UTC"
The following correctly parses the text input "2022-12-29 01:16:03 GMT+08:00".
public ZonedDateTime parseZonedDateTime(String timeStr) {
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss O");
ZonedDateTime zonedDateTime =…

Leil Jan C
- 85
- 8
4
votes
2 answers
Parsing LocalDate but getting DateTimeParseException; dd-MMM-uuuu
I am trying to convert a String to LocalDate using DateTimeFormatter, but I receive an exception:
java.time.format.DateTimeParseException: Text '2021-10-31' could not be parsed at index 5
My code is
DateTimeFormatter formatter =…

Arooshi
- 91
- 5
4
votes
4 answers
Kotlin date parsing is not working properly
So I have been looking up on how to properly parse an incoming datetime, problem is that this string contains the zone as well which apparently can't be parsed for some reason.
To give an example of the incoming date time…

Billy Cottrell
- 443
- 5
- 20
4
votes
1 answer
While parsing date with upper case month name I get java.time.format.DateTimeParseException
I have few lines of code as shown below
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd-MMM-yy");
formatter = formatter.withLocale( Locale.getDefault() );
LocalDate date = LocalDate.parse("11-NOV-20", formatter);
Which gives below…

JAVA_CAT
- 737
- 3
- 13
- 33
4
votes
1 answer
DateTimeFormatter.parse() corrects date information instead of throwing Exception
I'm using javac 15.0.1, on a Java SE8 project.
I have irregular input for LocalDate:
String fateDate = "1970-02-29";
With parse() of LocalDate I get this Exception (rightly so):
LocalDate.parse(fateDate);
Exception in thread "main"…

neverShown
- 43
- 3