problem with SimpleDateFormatter at end of year [duplicate] - java

This question already has answers here:
Difference between 'yy' and 'YY' in Java Time Pattern [duplicate]
(3 answers)
Y returns 2012 while y returns 2011 in SimpleDateFormat
(5 answers)
Closed 3 years ago.
Please take a look at the following code:
Date date = new GregorianCalendar (2019, Calendar.DECEMBER, 30).getTime ();
System.out.println (date);
SimpleDateFormat formatter = new SimpleDateFormat ("dd.MM.YYYY");
System.out.println (formatter.format (date));
The output is as follow:
Mon Dec 30 00:00:00 CET 2019
30.12.2020
Look at the year. What is wrong?
Same problem with '2019-12-31' while '2019-12-29' is ok.
Best regards
Thomas

Related

Java convert string to date not converting correctly [duplicate]

This question already has answers here:
Unable to parse date Apr 15, 2020 12:14:17 AM to LocalDatetime
(2 answers)
SimpleDateFormat producing wrong date time when parsing "YYYY-MM-dd HH:mm"
(5 answers)
simpledate formater to format returns incorrect.deducted date after formating [duplicate]
(2 answers)
Java Date() giving the wrong date [duplicate]
(9 answers)
Java SimpleDateFormat always returning January for Month
(4 answers)
Closed 2 years ago.
I have the following:
Date dateCommence = new SimpleDateFormat("YYYY-MM-DD HH:mm:ss").parse("2021-01-06 00:00:00");
But the dateCommence is the following:
Sun Dec 27 00:00:00 SAST 2020
Question
How do I convert the "2021-01-06 00:00:00" string to a date?
You were using the wrong date format mask. From the documentation, Y corresponds to the week year, and D is the day in year.
Try this version:
Date dateCommence = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
.parse("2021-01-06 00:00:00");
System.out.println(dateCommence);
This prints:
Wed Jan 06 00:00:00 CET 2021

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());

How can i set the calender instance getTime value back to the date object? [duplicate]

This question already has answers here:
Convert String to Calendar Object in Java
(8 answers)
How to parse a date? [duplicate]
(5 answers)
Closed 5 years ago.
First i have written the Calender instance getTime method value to a file. now by reading the file i want to set that value as date object?
Format of the data written to the file
Wed Mar 29 18:54:53 IST 2017
How can i do that?
Thanks!!
Check out the the class SimpleDateFormat: https://docs.oracle.com/javase/8/docs/api/java/text/SimpleDateFormat.html
This should work for your example:
String inputString = "Wed Mar 29 18:54:53 IST 2017";
SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy", Locale.ENGLISH);
sdf.parse(inputString)

Display date time in readable format [duplicate]

This question already has answers here:
How do I change date time format in Android?
(11 answers)
Closed 6 years ago.
I receive DATETIME from mysql database in a format like this 2016-10-12 18:52:00. Is there an easy way to print it in a readable like 12 October 2016 6:52 pm in java or android?
You can do things like:
SimpleDateFormat simpleDate = new SimpleDateFormat("MM/DD/YYYY HH:mm:ss");
Date date = new Date();
String S = simpleDate.format(date); //This will give you like 11/20/2016 08:45:02
Now use this form and get the month name programatically according to your need.

Categories