开发者

Convert DateString in a particular format

I have a problem that I have Date String in the format "2011-09-28T10:33:53.694+0000". I want to change this Date format in MM/DD/YYYY, don't know How? Please suggest me for right res开发者_C百科ult.


Something like this. Check the details for your "from" format.

DateFormat from = new SimpleDateFormat("yyyy-MM-ddThh:mm:ss");  // not sure about the details
from.setLenient(false);
DateFormat to = new SimpleDateFormat("MM/dd/yyyy");
to.setLenient(false);
Date d = from.parse(dateStr);
System.out.println(to.format(d));


String string = "2011-09-28T10:33:53.694+0000";
SimpleDateFormat inFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ", Locale.ENGLISH);
SimpleDateFormat outFormat = new SimpleDateFormat("MM/dd/yyyy", Locale.ENGLISH);
Date date;
String output;
try {
    date = inFormat.parse(string);
    output = outFormat.format(date); // 28/09/2011
} catch (ParseException e) {
    e.printStackTrace();
}


Do it this way ---->

  1. Create object Date class Date date = new Date();
  2. Create object of Simple Date Format class SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy"); Note -> MM - will give month in character and dd and yyyy will give date and year in numeric
  3. Convert date into string String s = sdf.format(date);

and you are done with it...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜