Java: format date string "150520" into "May 20, 2015" [duplicate] - java

This question already has answers here:
How to change the date format from YYMMDD to YYYY-MM-DD in java? [closed]
(2 answers)
Closed 5 years ago.
I'm using Java 8 on linux with the following code
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyMMDD",Locale.ENGLISH);
LocalDate exampleDate = LocalDate.parse(myDate, formatter);
where myDate is a String equal to "150520". I'm getting error:
java.time.format.DateTimeParseException: Text '150520' could not
be parsed: Conflict found: Field MonthOfYear 1 differs from
MonthOfYear 5 derived from 2015-01-20
I'd like to return May 20, 2015 for example. Any idea what's wrong?
UPDATE
Replacing the date code D with d eliminates this error. How to format into readable date?

You want yyMMdd. The uppercase D parses "day of year" not "day of month".
[Edit] for the printing part, you could do DateTimeFormatter.ofPattern("MMM dd, yyyy").format(theDate).

Change
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyMMDD",Locale.ENGLISH);
with
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyMMdd",Locale.ENGLISH);

You need to use this format string "yyMMdd". According to the docs
Symbol Meaning Presentation Examples
------ ------- ------------ -------
D day-of-year number 189
d day-of-month number 10

Related

Java SimpleDateFormat can not parse date in "yyyy-MM-dd'T'HH:mm:ss.SSSSSSSSSX" format pattern [duplicate]

This question already has answers here:
String-Date conversion with nanoseconds
(5 answers)
StringDate to Date coming in different Time in SimpleDateFormat in java
(2 answers)
Illegal pattern character 'T' when parsing a date string to java.util.Date
(4 answers)
Closed 1 year ago.
I have simple code, which parses string into date.
new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSSSSSSSX").parse("2021-06-28T07:09:30.463931900Z")
It parses this string into Sat Jul 03 18:01:41 CEST 2021, which is not valid.
When I remove from pattern 'SSSSSSSSSX', it starts working and returns: Mon Jun 28 07:09:30 CEST 2021.
Problem is that I need nanoseconds, so I can not just get rid of this.
I found many similar topics, but any of them dealt with such date format.
Use java.time:
You can parse this example String without any explicit pattern, keep the precision as desired and, if necessary, format those date and time values in a multitude of custom ways.
Here's a small example:
public static void main(String[] args) {
// example String (of ISO format)
String input = "2021-06-28T07:09:30.463931900Z";
// parse it (using a standard format implicitly)
OffsetDateTime odt = OffsetDateTime.parse(input);
// print the result
System.out.println(odt);
// if you want a different output, define a formatter
DateTimeFormatter dtf = DateTimeFormatter.ofPattern(
// use a desired pattern
"EEE MMM dd HH:mm:ss O uuuu",
// and a desired locale (important for names)
Locale.ENGLISH);
// print that
System.out.println(odt.format(dtf));
}
This code example produces the following output:
2021-06-28T07:09:30.463931900Z
Mon Jun 28 07:09:30 GMT 2021

DateTimeFormatter throwing exception "could not be parsed at index 0" [duplicate]

This question already has answers here:
Java - Unparseable date
(3 answers)
DateTimeFormatter month pattern letter "L" fails
(3 answers)
Closed 2 years ago.
I keep on getting an error from running this code.
java.time.format.DateTimeParseException: Text 'Jan 03, 2020' could not be parsed at index 0
final String myFormat = "LLL dd, yyyy"; //sets format in which to show date (same as how its saved in database) ex. Jan 29, 2020
final DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern(myFormat);
String startingBiWeeklyCheck = sharedPreferences.getString("biweekly start", "Jan 03, 2020");
LocalDate startingDate = LocalDate.parse(startingBiWeeklyCheck, dateFormatter);
Ive played around with the format but I'm not seeing why the pattern "LLL dd, yyyy" doesn't parse Jan 03, 2020
You should use MMM instead of LLL for month parsing.
Updated:
I was wrong about my answer above. It's the half of answer.
The deal is DateTimeFormatter.ofPattern(myFormat) uses default Locale.
For non-US locales, it doesn't work.
So you need to specify the locale according to your pattern.
DateTimeFormatter.ofPattern(myFormat).withLocale(Locale.US)

SimpleDateFormat Japanese time [duplicate]

This question already has answers here:
Java Date() giving the wrong date [duplicate]
(9 answers)
Y returns 2012 while y returns 2011 in SimpleDateFormat
(5 answers)
Invalid date is populated when we use yyyy-MM-dd'T'HH:mm:ssXXX format in java [duplicate]
(1 answer)
Closed 2 years ago.
The time I have is -> September 15 2020 11:10:25
I am using this code to format it in Japanese
timeFormatStr = "YYYY MMMMMMMMMM DD HH:mm:ss z";
SimpleDateFormat sd = new SimpleDateFormat(timeFormatStr, locale);
timeStr = sdf.format(new Date(time));
The timeStr looks like this (does not look right).
2020 9月 259 23:10:25 UTC
Any idea what the format string should be? I checked that the locale is - ja_JP.eucjp
Thanks
YYYY MMMMMMMMMM DD HH:mm:ss z is not how the Japanese format their dates and times. You should use DateTimeFormatter, and call ofLocalizedDateTime and withLocale. This will produce a formatter that produces strings in a native Japanese format.
String formatted = DateTimeFormatter
.ofLocalizedDateTime(FormatStyle.FULL) // choose a style here
.withLocale(Locale.JAPANESE)
.format(new Date(time).toInstant().atZone(ZoneOffset.UTC)); // choose a timezone here
System.out.println(formatted); // 1970年1月1日木曜日 0時00分00秒 Z
You shouldn't really be using Dates anymore. You should instead give the DateTimeFormatter a ZonedDateTime directly.

Parsing timestamp with '+' into Date or Instant in Java [duplicate]

This question already has answers here:
Java string to date conversion
(17 answers)
Closed 2 years ago.
I have timestamp of the format 2020-05-12 12:00:00+00 . How can I parse into Java.util.Date and Java.time.Instant.
Seemingly basic question, but I suspect it is the source of the problem that I am yet to solve in thread
Java 8 introduced DateTimeFormatter. Here is the link to the DateTimeFormatter Documentation.
For example:
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm:ssx");
String tsFromDb = "2020-05-12 12:00:00+00";
Instant inst = formatter.parse(tsFromDb, Instant::from);
System.out.println(inst);
Output:
2020-05-12T12:00:00Z
If you need a java.util.Date:
Date oldfashionedDate = Date.from(inst);
System.out.println(oldfashionedDate);
Output in America/Tijuana time zone:
Tue May 12 05:00:00 PDT 2020

Java-Zoned time not returning correct year [duplicate]

This question already has answers here:
Difference between 'YYYY' and 'yyyy' in NSDateFormatter
(4 answers)
Closed 3 years ago.
Due to the new year,I detected a bug in my project.
I am showing date and time in my order history using the following code:
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("h:mm a MM/dd/YY", Locale.US).withZone(ZoneId.systemDefault());
String formattedOrderDate = formatter.withZone(ZoneId.of(order.timeZone)).format(order.order.pickupAt);
textView.setText(formattedOrderDate );
Here are the values received from server:
order.order.pickupAt = {ZonedDateTime#8390} "2020-01-02T17:50Z"
order.timeZone = "America/Denver"
But the output is not showing the perfect year for the end of December:
As you can clearly see, year 2019 is showing as 2020.
But it is only showing for the last of December.Another order from mid of December is showing the correct date(year).
I am not able to detect what is going wrong over here.I am suspecting that this might be due to the timezone(America/Denver).But I have changed the timezone to my local timezone,still it is showing 2020 instead of 2019.
Use yy instead of YY
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("h:mm a MM/dd/yy", Locale.US).withZone(ZoneId.systemDefault());
String formattedOrderDate = formatter.withZone(ZoneId.of(order.timeZone)).format(order.order.pickupAt);
textView.setText(formattedOrderDate );
YY is for week-based calendar year and yy is for calendar year. Last few days of December was actually the first week of 2020, so it is showing 2020 for those days.
Change your code to use yy, then it will show correct year -
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("h:mm a MM/dd/yy", Locale.US).withZone(ZoneId.systemDefault());

Categories