我从XML解析获取日期字符串如下:2012-04-05 07:55:29 +05.30
现在,我希望这个字符串为:05-April-2012
我该怎么做?
答案 0 :(得分:8)
首先解析这个到目前为止。
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());
答案 1 :(得分:2)
你可以单独拆分所有内容,并按照你想要的格式进行格式化;
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;
}
答案 2 :(得分:2)
考虑,
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
答案 3 :(得分:1)
使用("dd-MMMMMMMMM-yyyy")
作为DateStringFormat