I'm very new to Java programming and I have string like this:
2013-03-15T07:23:13Z
I wish I could convert this into date format like:
15-03-2013
is that possible?
Thanks in advance.
Take the reference to this link
How can I change the date format in Java?
See the answer given by Mr. Christopher Parker
It has explained all your needs and it will provide you the easiest solution which is logically correct
Try this :
try {
DateFormat sourceDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
DateFormat targetFormat = new SimpleDateFormat("dd-MM-yyyy");
String strSourceDate = "2013-03-15T07:23:13Z";
Date targetDate = (Date) sourceDateFormat.parseObject(strSourceDate);
String strTargetDate = targetFormat.format(targetDate);
System.out.println(strTargetDate);
} catch (ParseException e) {
e.printStackTrace();
}
If the format of the input string is fixed, the simplest and the most expedient way of doing this would be with string manipulation:
String s = "2013-03-15T07:23:13Z";
String res = s.substring(8, 10)+"-"+s.substring(5, 7)+"-"+s.substring(0, 4);
It would spare you dealing with dates and calendars. Here is a demo on ideone.
Try this:
Date dNow = new Date( );
SimpleDateFormat ft =
new SimpleDateFormat ("dd.MM.yyyy");
System.out.println("Current Date: " + ft.format(dNow));
It's output
Current Date: 15.03.2013
java.text.SimpleDateFormat
is what you need: SimpleDateFormat JavaDoc
You'll need one format to convert your input String
into a Date
using the parse()
method, and then another to convert that Date
into a String
in your desired format using format()
.
If your application could be used internationally, don't forget to think about correctly localizing the output of the second function though. 03-11-2013 is March 11th in some countries, November 3rd in others.
Using SimpleDateFormat
String strDate = "2013-03-15T07:23:13Z";
SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");
String date = dateFormat.format(strDate);
System.out.println("Today in dd-MM-yyyy format : " + date);
Hope it help you...