0

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.

batman
  • 4,728
  • 8
  • 39
  • 45

6 Answers6

2

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

Community
  • 1
  • 1
2

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();
}
Omar MEBARKI
  • 647
  • 4
  • 8
1

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.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
0

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
Apb
  • 979
  • 1
  • 8
  • 25
0

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.

Dave Mulligan
  • 1,623
  • 3
  • 19
  • 31
0

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...

Eric
  • 248
  • 2
  • 8
  • 21