I have a date, "2015-06-08". I want convert it to "08 June 2015"
String oldDateString = "2015-06-08";
SimpleDateFormat oldDateFormat = new SimpleDateFormat("yyyy-mm-dd", Locale.getDefault());
SimpleDateFormat newDateFormat = new SimpleDateFormat("dd MMMM yyyy", Locale.getDefault());
Date date = oldDateFormat.parse(oldDateString);
String result = newDateFormat.format(date);
but this does not work correctly. It returns "08 January 2015". What have I done wrong?
Use yyyy-MM-dd insted yyyy-mm-dd because 'm' in lowercase is minutes.
Documentation : link
Related
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 a date time (which is a string) in the following format: 2/19/2015 5:25:35 p.m, and I wanted to turn it in the following Date Format: Thu Feb 19 5:25:35 p.m. CET 2015 I tried the following code:
String sDatePrecedenteExecution = "19/02/2015 17:30:29";
SimpleDateFormat format = new SimpleDateFormat ("ddd d mmm yyyy HH: mm: ss");
Date date = format.parse (sDatePrecedenteExecution)
but I got the following error:
java.text.ParseException: unparseable Date: "2/19/2015 5:30:29 p.m."
Has java.text.DateFormat.parse (DateFormat.java:337)
You are currently using the "output" format to read your incoming date string (2/19/2015 5:25:35 p.m), which is why you see the error.
You need to specify a second format for parsing your incoming date string, and use that format to parse instead. It should look like this:
SimpleDateFormat inFormat = new SimpleDateFormat ("dd/MM/yyyy HH:mm:ss")
Date date = inFormat.parse(sDatePrecedenteExecution)
Note that you also have a bug in your output format - m means minutes, and you want MMM, which is months. Have a look at the docs.
Your SimpleDateFormat doesn't match the format which you are entering. They should reflect the same.
Try this code
String parseDate = ""19/02/2015 17:30:29";
SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
Date parsedDate = dateFormat.parse(parseDate);
You need to change your code something like...
String sDatePrecedenteExecution = "19/02/2015 17:30:29";
SimpleDateFormat format = new SimpleDateFormat ("dd/mm/yyyy HH:mm:ss");
try {
Date date = format.parse (sDatePrecedenteExecution);
System.out.println(date);
format = new SimpleDateFormat ("ddd d mmm yyyy HH: mm: ss");
String str = format.format(date);
System.out.println(str);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Try this pattern:
SimpleDateFormat format = new SimpleDateFormat ("dd mm yyyy HH:mm:ss");
You went wrong when you made: "ddd d mmm yyyy HH: mm: ss"
Use this pattern "dd/M/yyyy HH:mm:ss" instead & read the documentation
SimpleDateFormat format = new SimpleDateFormat ("dd/M/yyyy HH:mm:ss");
String sDatePrecedenteExecution = "19/02/2015 17:30:29";
try{date =format.parse (sDatePrecedenteExecution);
}catch(Exception ex){//deal with it here}
System.out.println(date.toString()); //Thu Feb 19 17:30:29 UTC 2015
I want the convert the string like 1/15/2014 9:57:03 AM or 1/15/2014 5:49:39 PM to 9:57:03 and 17:49:39
SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss a", Locale.US);
Date date = format.parse("1/15/2014 9:57:03 AM");
format = new SimpleDateFormat("HH:mm:ss");
String dateString = format.format(date);
BTW, use Google next time
You should see this in the doc
doc SimpleDateFormat
I have my date and time stored in string i.e my string contains str ="18/01/2013 5:00:00 pm". How can I convert it to 24 format time in android?
You can use two SimpleDateFormat instances: one to parse the input as a date and a second to format the date as a string with the desired format.
For example, formattedDate in the code below will be 18/01/2013 17:00:00:
String str = "18/01/2013 5:00:00 pm";
SimpleDateFormat input = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss a");
Date dt = input.parse(str);
SimpleDateFormat output = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
String formattedDate = output.format(dt); //contains 18/01/2013 17:00:00
Notes:
hh is for Hour in am/pm (1-12) whereas HH is for Hour in day (0-23).
for more formatting options, check the javadoc
try
String str ="18/01/2013 5:00:00 pm";
Date date = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss a").parse(str);
str = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss").format(date);
System.out.println(str);
output
18/01/2013 17:00:00
To get AM PM and 12 hour date format use hh:mm:ss a as string formatter WHERE hh is for 12 hour format and a is for AM PM format.
Note: HH is for 24 hour and hh is for 12 hour date format
SimpleDateFormat formatter = new SimpleDateFormat("mm/dd/yyyy hh:mm:ss a");
String newFormat = formatter.format(testDate);
Example
String date = "18/01/2013 5:00:00 pm";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/mm/dd HH:MM:SS");
Date testDate = null;
try {
testDate = sdf.parse(date);
}catch(Exception ex){
ex.printStackTrace();
}
SimpleDateFormat formatter = new SimpleDateFormat("mm/dd/yyyy hh:mm:ss a");
String newFormat = formatter.format(testDate);
System.out.println(".....Date..."+newFormat);
You can use SimpleDateFormat for that:
SimpleDateFormat dateFormat = new SimpleDateFormat(dd/mm/yyyy HH:mm:ss");
Refer to this which was opposite to your requirement. Just posted the link so you might get the idea of difference that HH and hh makes.
Try using the java SimpleDateFormat class.
Example:
SimpleDateFormat df = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
df.parse(date);
The upper case HH use a 24h format
How can I convert dates with full month names into a Date object in Java? Here's a sample date that I need to convert: 6 December 2002.
Thanks.
String str = "6 December 2002";
DateFormat formatter = new SimpleDateFormat("dd MMM yyyy");
Date date = (Date)formatter.parse(str);
Here is working IDE One demo
Must See
API Doc
Have a look at SimpleDateFormat
SimpleDateFormat sdf = new SimpleDateFormat("d MMMMM yyyy", Locale.US);
Date d = sdf.parse("6 December 2002");
System.out.println(d);