String dateString="2001/03/09";
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/mm/dd");
Date convertedDate = dateFormat.parse(dateString);
System.out.println("Converted string to date : " + convertedDate);
i get output as follows:
Converted string to date : Tue Jan 09 00:03:00 IST 2001
What is wrong with my code?
Use MM instead of mm for months - mm means minutes, not months.
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd");
See the API documentation of SimpleDateFormat.
Related
I am getting time in a particular format which I want to convert to another format. The below code snippet was taken from one of the stack overflow answers.
SimpleDateFormat origDateFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm");
SimpleDateFormat newDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
Date retrivedDate = origDateFormat.parse(origFormat);
return newDateFormat.format(retrivedDate);
This code format is working as long as my input date is of the following format: 22/08/2015 14:23.
But when i try with seconds, i get the error:
Unparseable date: "06/03/2019 14:17:25"
SimpleDateFormat origDateFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
SimpleDateFormat newDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date retrivedDate = origDateFormat.parse(origFormat);
return newDateFormat.format(retrivedDate);
Here's the javadoc of parse() method and this is what it says:
Parses text from the beginning of the given string to produce a date.
The method may not use the entire text of the given string.
So, as per the documentation, you can parse 06/03/2019 14:17:25 dates with both dd/MM/yyyy HH:mm and dd/MM/yyyy HH:mm:ss date formats. If you parse with the format that doesn't contain seconds then it will ignore the second part. Below is the working code:
String date = "06/03/2019 14:17:25";
SimpleDateFormat mmDateFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm");
SimpleDateFormat ssDateFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
System.out.println(mmDateFormat.parse(date));
System.out.println(ssDateFormat.parse(date));
Below are the outputs:
Wed Mar 06 14:17:00 GMT 2019
Wed Mar 06 14:17:25 GMT 2019
I want to convert a string to date before storing it and I used
SimpleDateFormat format = new SimpleDateFormat("yyyy-mm-dd");
Date returnDate = format.parse(date);
When I ran this with sample date:
the input string for date conversion is 2014-05-06
the parsed date is Mon Jan 06 00:05:00 IST 2014
now when I store the returnDate in MySql the value is 2014-01-06 00:05:00
Why is the date changed ? Want to know if I am missing something. I went through the posts related to date string conversion : How to convert a date from a Datepicker to Mysql DATETIME format using java?
In your DateFormat use MM for month instead of mm, that is for minutes
Reference: http://docs.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html
You can use like this :
Date mDate= new Date(System.currentTimeMillis());
SimpleDateFormat mDateFormat= new SimpleDateFormat("dd MMM yyyy HH:mm a");
String dateformat=mDateFormat.format(mDate);
the string ["dd MMM yyyy HH:mm a"] can be changed according to need of formate.
Like in your case : "yyyy-mm-dd
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 want to convert the timestamp Wed, 20 Feb 2013 11:41:23 GMT to 2013-02-20T11:41:23Z . How can I do this? I want to ISO-8601 in UTC format(2013-20-02T04:51:03Z).
My code is below
Date date=new Date();
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
String strDate = date.toString();
Date dt = formatter.parse(strDate );
System.out.println("Date " +dt);
Output is:
Exception in thread "main" java.text.ParseException: Unparseable date: "Wed Feb 20 03:50:03 PST 2013"
Its your format what is wrong, use:
"EEE MMM dd HH:mm:ss Z yyyy" that is what is comming out of the exception, in the question you have "EEE, dd MMM yyyy HH:mm:ss Z" for Wed, 20 Feb 2013 11:41:23 GMT
Try:
Date date = new Date();
SimpleDateFormat formatter = new SimpleDateFormat(
"EEE MMM dd HH:mm:ss Z yyyy");
String strDate = date.toString();
Date dt = null;
try {
dt = formatter.parse("Wed Feb 20 03:50:03 PST 2013");
} catch (ParseException e) {
e.printStackTrace();
}
System.out.println("Date " + dt);
System.out.println(new Timestamp(new Date().getTime()));
Consider using Joda Time, which has built in support for parsing and outputting ISO-format date strings.
new DateTime(DateTimeZone.UTC).toString()
You need 2 DateFormatters, one for parsing and one for output. You have the one for output.
EDIT: Output works like this:
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
String output = formatter.format(new Date());
System.out.println("Date " + output);
You already have a Date object. You just need to format it to String.
formatter.format(date) should give you the desired result if the pattern in the SimpleDateFormat constructor is valid.
With the above implementation your code looks like this:
Date date = new Date();
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
String dt = formatter.format(date);
System.out.println("Date " + dt);
Which results in an output like - Date 2013-02-20T17:39:45Z.
DateFormat format = new SimpleDateFormat ("yyyy-MM-dd'T'HH:mm:ss'Z'");
format.setTimeZone (TimeZone.getTimeZone ("UTC"));
Date date = new Date ();
System.out.println ("Date is: " + format.format (date));
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));