This question already has answers here:
How to convert a String to a Date using SimpleDateFormat?
(10 answers)
Closed 7 years ago.
Update:
Really?!!! Duplicate??? My format is correct (yyyy/MM/dd HH:mm:ss) but return time is incorrect. How this is similar to another question????
I'm trying to create java Date but it's always return wrong value. This is my code:
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:MM:SS");
Date GreDate = dateFormat.parse("2014/03/22 00:00:00");
And GreDate return Sun Dec 22 00:00:00 GMT+03:30 2013 as value.
Please don't suggest to use external library for date type.
Update:
I changed my pattern to this:
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Now GreDate returns Sat Mar 22 01:00:00 GMT+04:30 2014. Year is correct but time still not 00:00:00.
Note that:
MM are the months, mm are the minutes.
SS are the milliseconds, ss are the seconds.
So you need to change your dateFormat to
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Basically there are two errors in your pattern, both in the time part (seconds and minutes).
Here is the link to the complete documentation link.
Related
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.
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());
This question already has answers here:
How to set the TimeZone for String parsing in Android
(3 answers)
Closed 6 years ago.
I get from the server is like 2017-01-24T16:16:30.690Z.
This date is in GMT time zone.
I want to convert this time into GMT+6 time zone as well as time format.
My expected result is: 24 January 2017 22:16
See above comments. If you apply those, try:
SimpleDateFormat sdf = new SimpleDateFormat();
sdf.applyPattern("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
Date date = sdf.parse("2017-01-24T16:16:30.690Z", new ParsePosition(0));
sdf.setTimeZone(TimeZone.getTimeZone("GMT+06:00"));
sdf.applyPattern("d MMMM yyyy HH:mm");
String formatted = sdf.format(date);
Worked for me.
This question already has answers here:
Change date format in a Java string
(22 answers)
Closed 7 years ago.
I am working on a Java application and I have the following problem trying to format a date.
So I inizialize an object field with a new java.util.Date; that represent the current date time, this one:
progetto.setDatOraUltMov(new Date());
When I print this field the result is somethind like this:
Mon Oct 12 17:19:06 CEST 2015
Ok, this standard is not good for my pourpose and I want that is shown something like this:
12/10/2015 17:19:06
Something like in the format DAY/MONTH/YEAR HOUR:MINUTE:SECOND
How can I do something like this? How can I specify the required date format?
Use SimpleDateFormat.
SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd hh:mm:ss");
DateToStr = format.format(curDate);
For your reference
http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html
You can try this
Calendar currentDate = Calendar.getInstance();
SimpleDateFormat formatter= new SimpleDateFormat("dd-MM-YYYY-hh:mm:ss");
String dateNow = formatter.format(currentDate.getTime());
System.out.println(dateNow);
import
import java.text.SimpleDateFormat;
import java.util.Calendar;
This question already has answers here:
Change date format in a Java string
(22 answers)
Closed 8 years ago.
I am trying from half an hour to convert string to date by using following code:
SimpleDateFormat dateFormat = new SimpleDateFormat("YYYY-MM-dd");
Date lastCharged = dateFormat.parse(lastChargeDate);
Every time I run this code the date returned by the system is Sun Dec 29 00:00:00 PKT 2013 Even if i changed the date manually same is the response by the system.
Any help in this regard a lot of work is suspended just because of this blunder.
DateFormat#parse() method just convert the String to Date. It doesn't change anything in the converted Date it means it doesn't store the format from which it is constructed.
Whenever you print the Date object again then it prints in its default toString() implementation that's what you are getting.
It you need to print it again in specific format then use DateFormat#format() method.
The format should be yyyy-MM-dd instead of YYYY-MM-dd.
Sample code:
String oldDate="2014-06-07";
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
Date date=dateFormat.parse(oldDate);
System.out.println(date);
String newDate=dateFormat.format(date);
System.out.println(newDate);
output:
Sat Jun 07 00:00:00 IST 2014
2014-06-07