我从 XML 解析中获取日期字符串,如下所示:2012-04-05 07:55:29 +05.30
现在,我希望这个字符串为:05-April-2012
我怎样才能做到这一点?
我从 XML 解析中获取日期字符串,如下所示:2012-04-05 07:55:29 +05.30
现在,我希望这个字符串为:05-April-2012
我怎样才能做到这一点?
首先解析这个迄今为止。
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());
您可以单独拆分所有内容,然后按照您的意愿进行格式化;
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;
}
考虑,
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
用作("dd-MMMMMMMMM-yyyy")
DateStringFormat