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
Related
This is both a duplicate and not a duplicate!
Just please help me and don't refer me to anywhere else, cause I'm really unable to get the GMT time.
The answer seems easy but it doesn't work for me.
I don't know are the answers all aver the web wrong, or am I making a mistake?
Please take a look at this snippet and the results:
Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
long time = cal.getTimeInMillis();
Date date = new Date(time);
System.out.println(date);
time = System.currentTimeMillis();
date = new Date(time);
System.out.println("--\n" + date);
result :
Fri Feb 28 16:07:12 GMT+03:30 2014--
Fri Feb 28 16:07:12 GMT+03:30 2014
Both show my local time. I even printed directly the time, cause I thought maybe this is due to Date class but even those are the same (with just about 1 or 2 milliseconds difference).
Use setTimeZone() on a SimpleDateFormat to print the date in a specific timezone.
DateFormat format = new SimpleDateFormat("dd-MMM-yyyy HH:mm:ss z");
Date d = format.parse("28-Feb-2014 13:00:00 PST");
System.out.println(format.format(d));
format.setTimeZone(TimeZone.getTimeZone("GMT"));
System.out.println(format.format(d));
Prints:
28-Feb-2014 13:00:00 PST
28-Feb-2014 21:00:00 GMT
Try the below
SimpleDateFormat f = new SimpleDateFormat("yyyy-MMM-dd HH:mm:ss");
f.setTimeZone(TimeZone.getTimeZone("GMT"));
System.out.println(f.format(new Date(System.currentTimeMillis())));
One way of doing it is using SimpleDateFormat and the setTimeZone method():
SimpleDateFormat sdf = new SimpleDateFormat( "..." );
sdf.setTimeZone( TimeZone.getTimeZone( "GMT" ) ); // or UTC
System.out.println( sdf.format( date ) );
Cheers,
I have time on device 11:34
SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy', 'hh:mm");
Date date_current = new Date();
Date date_start = null;
date_start = sdf.parse("12.03.2014, 12:00");// I PARSE THIS DATE!!!
RESULT IS :
date_start:
Wed Mar 12 00:00:00 Восточноевропейское время 2014
BUT SHOULD BE:
Wed Mar 12 12:00:00 Восточноевропейское время 2014
HOW to solve it?
To get 24h format use HH not hh. In 12h format hours can be in rage 0-11, which makes 12 overflow to 0.
Use
SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy', 'HH:mm");
use 24 hour date format
SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy', 'HH:mm");
First of all take a look about patterns of Simpledatrformat. where it clearly shows H is for (0-23).
Reference for Date Format Pattern Syntax
so you should change your code like below.
SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy', 'HH:mm");
Date date_current = new Date();
Date date_start = null;
date_start = sdf.parse("12.03.2014, 12:00");
System.out.println("now time is.." + date_start);
OR
Use this:
Date date = new Date();
date.setHours(date.getHours() + 8);
System.out.println(date);
SimpleDateFormat simpDate;
simpDate = new SimpleDateFormat("kk:mm:ss");
System.out.println(simpDate.format(date));
Thanks.. use above code to parse correctly!!
I have used the following
SimpleDateFormat df = new SimpleDateFormat("dd-mm-yyyy hh:mm:ss zzz");
Date date = new Date();
String formattedDate= df.format(date);
Date dateWithTime = df.parse(formattedDate);
i got the formatted date as string when i conver this into date i got error like
java.text.ParseException: Unparseable date: "Tue Feb 26 11:45:43 IST 2013"
How would convert to date or how i format a current date and get as date?
I think your code wouldn't throw ParseException. But it sure would definitely yield wrong output. your format should be:
"dd-MM-yyyy hh:mm:ss zzz"
Note that
MM---> Months
mm---> Minutes
Test with your code with out correcting the format:
Sat Jan 26 06:24:07 GMT 2013
Test with your code with correcting the format:
Tue Feb 26 06:20:51 GMT 2013
The date format should be as follows as shown in the exception. Change it to -
EEE MMM d hh:mm:ss z yyyy
The correct simpledateformat will be
SimpleDateFormat df = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
Please refer the link for proper date formatting and parsing
SimpleDateFormat
I need to change the input date format to my desired format.
String time = "Fri, 02 Nov 2012 11:58 pm CET";
SimpleDateFormat displayFormat =
new SimpleDateFormat("dd.MM.yyyy, HH:mm");
SimpleDateFormat parseFormat =
new SimpleDateFormat("EEE, dd MMM yyyy HH:mm aa z");
Date date = parseFormat.parse(time);
System.out.println("output is " + displayFormat.format(date));
it gives me this error
java.text.ParseException: Unparseable date: "Fri, 02 Nov 2012 11:58 pm CET"
at java.text.DateFormat.parse(Unknown Source)
at Main.main(Main.java:10)
Can anyody help me? Because this code doesn't work.
It appears Android's z does not accept time zones in the format XXX (such as "CET"). (Pulling from the SimpleDateFormat documentation.)
Try this instead:
String time = "Fri, 02 Nov 2012 11:58 pm +0100"; // CET = +1hr = +0100
SimpleDateFormat parseFormat =
new SimpleDateFormat("EEE, dd MMM yyyy hh:mm aa Z"); // Capital Z
Date date = parseFormat.parse(time);
SimpleDateFormat displayFormat =
new SimpleDateFormat("dd.MM.yyyy, HH:mm");
System.out.println("output is " + displayFormat.format(date));
output is 02.11.2012, 22:58
Note: Also, I think you meant hh instead of HH, since you have PM.
Result is shown here. (This uses Java7's SimpleDateFormat, but Android should support RFC 822 timezones (+0100) as well.)
NB: Also, as it appears Android's z accepts full names ("Pacific Standard Time" is the example they give), you could simply specify "Centural European Time" instead of "CET".
Try out the following code:
SimpleDateFormat date_format = new SimpleDateFormat("yyyyMMMdd");
System.out.println(date_format.format(cal.getTime()));
It will work.. If not print the log cat? What erroe is coming?
First of All I must agree with #Eric answer.
You just need to remove "CET" from your string of date.
Here is sample code. Check it.
String time = "Fri, 02 Nov 2012 11:58 pm CET";
time = time.replaceAll("CET", "").trim();
SimpleDateFormat displayFormat =
new SimpleDateFormat("dd.MM.yyyy, HH:mm");
SimpleDateFormat parseFormat =
new SimpleDateFormat("EEE, dd MMM yyyy HH:mm aa");
Date date = null;
try {
date = parseFormat.parse(time);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("output is " + displayFormat.format(date));
I have a string (ex: 2012-30-03 12:30), and I am trying to use the following code:
DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm");
this.startTime = (Date) df.parse(startTime); // startTime = "2012-30-03 12:30"
But when I try the following:
System.out.println(this.startTime);
The following get printed
Tue Jun 03 12:30:00 CEST 2014
What the heck is wrong here?
Is there a better way to turn the kind of string I have into a working date object?
I think
DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm");
should be
DateFormat df = new SimpleDateFormat("yyyy-dd-MM HH:mm");
based on the example string you provided (2012-30-03 12:30).
Focus your rage against the date class into verifying the basics and you'll be ok. ;)