I wanted to convert String to Date. My code:
String maturityDate = "20150722";
SimpleDateFormat formatter = new SimpleDateFormat("yyyymmdd");
Date date = formatter.parse(maturityDate);
System.out.println(date);
Expected Result :
Input 20150722
Output Wed Jul 22 00:07:00 IST 2015
Actual Result :
Input 20150722
Output Thu Jan 22 00:07:00 IST 2015
What could be the cause?
What could be the cause?
Cause is m letter means minute in SimpleDateFormat pattern. You mean M for months.
SOLUTION
Change your format from yyyymmdd to yyyyMMdd.
SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
In the API you will find the complete list:
M Month in year Month July; Jul; 07
m Minute in hour Number 30
Related
Using this code:
DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.FULL, new Locale("no")).format(Date);
Date 3 AM CST 12 DEC 16 would have a result of: 12. desember 2016 kl 03.00 CST
But what if I only want to show the hours in the time format and remove the minutes and seconds if ever it was present on other locales?
Expecting a result of 12. desember 2016 kl 03 CST or if using English locale then should only be December 12, 2016 3 AM CST
How about having a SimpleDateFormatter like below:
SimpleDateFormat dateFormat = new SimpleDateFormat("dd. MMMMM yyyy aa hh z", new Locale("no"));
System.out.println(dateFormat.format(new Date()));
http://ideone.com/T5wSRV this is the link to below code
SimpleDateFormat dateFormatIST = new SimpleDateFormat("yyyy-MMM-dd HH:mm:ss");
dateFormatIST.setTimeZone(TimeZone.getTimeZone("IST"));
//Time in IST
Date date=dateFormatIST.parse( dateFormatIST.format(new Date()) );
System.out.println(date);
this is not giving correct IST time where as code below is working fine . why?
http://ideone.com/9KSaZx this is the link to below code which is giving the desired output.Help me understand the behavior.
SimpleDateFormat dateFormatIST = new SimpleDateFormat("yyyy-MMM-dd HH:mm:ss");
dateFormatIST.setTimeZone(TimeZone.getTimeZone("IST"));
//Local time zone
SimpleDateFormat dateFormatLocal = new SimpleDateFormat("yyyy-MMM-dd HH:mm:ss");
//Time in IST
Date date=dateFormatLocal.parse( dateFormatIST.format(new Date()) );
System.out.println(date);
The behaviour is logical. The point is that there is no information of time-zone is a Date object. A Date object contains Universal Time.
And when you format then parse the formatted string, you still have the same date:
I commented the code with the results:
SimpleDateFormat dateFormatIST = new SimpleDateFormat("yyyy-MMM-dd HH:mm:ss");
dateFormatIST.setTimeZone(TimeZone.getTimeZone("IST"));
//Local time zone
SimpleDateFormat dateFormatLocal = new SimpleDateFormat("yyyy-MMM-dd HH:mm:ss");
//Time in IST
Date d = new Date();
System.out.println(d);
// Mon Mar 16 16:57:19 CET 2015
=> now in my TZ (CET)
System.out.println(dateFormatIST.format(d));
// 2015-Mar-16 21:27:19
=> now in IST TZ
System.out.println(dateFormatLocal.format(d));
// 2015-Mar-16 16:57:19
=> now in my TZ (CET)
Date dateIST = dateFormatIST.parse(dateFormatIST.format(d));
System.out.println(dateIST);
// Mon Mar 16 16:57:19 CET 2015
=> The dateIST object contains still "now", and the format is default local which is CET
Date dateLoc = dateFormatLocal.parse(dateFormatLocal.format(d));
System.out.println(dateLoc);
// Mon Mar 16 16:57:19 CET 2015
=> same thing as above
Date dateLocIST = dateFormatLocal.parse(dateFormatIST.format(d));
System.out.println(dateLocIST);
// Mon Mar 16 21:27:19 CET 2015
=> dateFormatIST.format(d) gives "2015-Mar-16 21:27:19", and dateFormatLocal.parse() will interpret it like a local (CET for me) date. The result is then "Mon Mar 16 21:27:19 CET 2015".
If you need to translate dates between different time-zone, you certainly need to go for the Calendar class.
I am probably overlooking something, but parsing from string to date is not working correctly for me.
I have String: "20110705_060229" which is format: "YYYYddMM_HHmmss"
this piece of code:
Date triggermoment;
public void setTriggermoment(String triggermoment) throws ParseException {
DateFormat formatter = new SimpleDateFormat("YYYYddMM_HHmmss");
this.triggermoment = formatter.parse(triggermoment);
}
gives me as output (when I run toString() method on triggermoment):
Mon Jan 03 06:02:29 CET 2011
Why is it not parsing the day's and month's correctly? It should be June 7 instead of Jan 3.
Thanks in advance!
You have to use y for years. Y is used for week year :
DateFormat formatter = new SimpleDateFormat("yyyyddMM_HHmmss");
Output:
Sat May 07 06:02:29 CEST 2011
It should be June 7 instead of Jan 3
The 5th month in the year is may, not june.
MM is month whereas mm is munutes. y is for year and Y is used for Week year.
Try yyyyddMM_HHmmss instead of YYYYddMM_HHmmss
I have this piece of simple code:
SimpleDateFormat sqlFormatter = new SimpleDateFormat ("YYYY-MM-dd HH:mm:ss");
String temp = "2012-03-09 12:00:00";
System.out.println (temp);
Date last = sqlFormatter.parse (temp);
System.out.println (last);
I get this output:
2012-03-09 12:00:00
Sun Jan 01 12:00:00 EST 2012
I know is supposed to be simple, but I am hoping someone can quickly see what I am missing.
I think your pattern is a little off. I'm suprised you're not seeing an IllegalArgumentException. Try using the following pattern with lower case y's and see if that resolves your issue:
SimpleDateFormat sqlFormatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Corrected code here:
SimpleDateFormat sqlFormatter = new SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss");
String temp = "2012-03-09 12:00:00";
System.out.println(temp);
Date last = sqlFormatter.parse(temp);
System.out.println(last);
You should have SimpleDateFormat instead of SimpleDateFormatter and for years you give yyyy instead of YYYY.
Once I corrected your format String - Y is not allowed, you need y - (and the typo already mentioned) it worked fine:
SimpleDateFormat sqlFormatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String temp = "2012-03-09 12:00:00";
System.out.println (temp);
Date last = sqlFormatter.parse (temp);
System.out.println (last);
>2012-03-09 12:00:00
>Fri Mar 09 12:00:00 EST 2012
You need to use 'yyyy' and not 'YYYY'
Here is the output
2012-03-09 12:00:00
Fri Mar 09 12:00:00 IST 2012
for the pattern
yyyy-MM-dd HH:mm:ss
What is the date format to get only hours in 12-hours format from this time
Thu Oct 20 13:12:00 GMT+02:00 2011
edit:
using this code
Date eventDate = tempAppointments.get(i).mStartDate
System.out.println(eventDate.toString());
// date pattern
DateFormat df = new SimpleDateFormat("hh:'00' a");//output : Wed Nov 09 11:00:00 GMT+02:00 2011
// get the start date with new format (pattern)
String hours = df.format(tempAppointments.get(i).mStartDate.getDay());
System.out.print(hours);//output: 02:00 AM
return hours as
02:00 AM
but for the given time. it must be 02:00 PM . why ?
I'm not sure why you are passing date.getDay() (which is deprecated, by the way) into the formatter if you want the hour part.
Try this:-
Date date = new Date();
System.out.println("Date: " + date);
DateFormat df = new SimpleDateFormat("hh:'00' a");
String hour = df.format(date);
System.out.println("Hour: " + hour);
The output:
Date: Sat Nov 19 17:57:05 CST 2011
Hour: 05:00 PM
Date fecha = new Date();
System.out.println("Fecha "+fecha);
DateFormat formato = new SimpleDateFormat("hh:mm:ss");
String hora = formato.format(fecha);
System.out.println("Son las "+hora);