-1

I am getting date string from XML parsing like this: 2012-04-05 07:55:29 +05.30

Now, I want this string as : 05-April-2012

How can I do this?

Selvin
  • 6,598
  • 3
  • 37
  • 43
Amith
  • 127
  • 1
  • 4
  • 11

4 Answers4

8

First parse this to date.

String time = "Sun Jul 15 2012 12:22:00 GMT+03:00 (FLE Daylight Time)";
SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd yyyy HH:mm:ss zzz");
Date date = sdf.parse(time);


SimpleDateFormat sdf=new SimpleDateFormat("dd-MMM-yyyy");
String s=sdf.format(date.getTime());
zacharia
  • 1,083
  • 2
  • 10
  • 22
2

you can split everything seperately, and format however you want like that;

public String getDateFromString(String dateString){
    if(dateString!=null){
        String[] dateRoot=dateString.split(" ");
        String ymd=dateRoot[0];
        String hms= dateRoot[1];
        String[] calRoot=ymd.split("-");
        int year=Integer.parseInt(calRoot[0]);
        int month=Integer.parseInt(calRoot[1]);
        int day=Integer.parseInt(calRoot[2]);

        String[] timeRoot=hms.split(":");
        int hour=Integer.parseInt(timeRoot[0]);
        int minute=Integer.parseInt(timeRoot[1]);
        int second=Integer.parseInt(timeRoot[2]);

        String newFormat =  day+"-"+month+"-"+year;

        return newFormat;
        }
    return null;
}
canova
  • 3,965
  • 2
  • 22
  • 39
2

consider,

String date="2012-04-05 07:55:29 +05.30";
//split the above string based on space
String[] dateArr=date.split(" ");

//Now in dateArr[0] you will have 2012-04-05, split this based on "-" to get yy,mm,dd
String[] yymmdd=dateArr[0].split("-");

//Now in get month name using a String array
String months[12]={"Jan","Feb","Mar","April","May","June","July","Aug","Sept","oct","Nov","Dec"};

//Now index to the above array will be your yymmdd[1]-1 coz array index starts from 0
String yy=yymmdd[0];
String dd=yymmdd[2];
String mm=months[Integer.parseInt(yymmdd[1])-1];

//Now you have the dd,mm,and yy as you need, So you can concatenate and display it

String myDate=dd+"-"+mm+"-"+yy;

//myDate will have the string you need
Zax
  • 2,870
  • 7
  • 52
  • 76
1

use ("dd-MMMMMMMMM-yyyy") as a DateStringFormat

Selvin
  • 6,598
  • 3
  • 37
  • 43
Onur A.
  • 3,007
  • 3
  • 22
  • 37
  • 1
    what for? ... i realy don't care ... i'm just pointing that there is no such format in java like `MMMMMMMMM` ... are you writing an answers only to write something? – Selvin Jul 16 '13 at 11:24
  • 1
    no you point me that there was no such format in java like that, then i'm asking you to write the correct one, btw take a look at this before downvote http://www.java2s.com/Tutorial/Java/0040__Data-Type/DateFormatwithSimpleDateFormat.htm – Onur A. Jul 16 '13 at 12:20
  • i had change my vote since it's working but still `MMMM` for full month is enough(this is not my -1) – Selvin Jul 16 '13 at 14:20
  • thanks for change, but it still seems like -1, you sure changed it ? – Onur A. Jul 16 '13 at 14:24