如何转换日期格式

Sri*_*ddy 0 java

任何人都可以帮助转换日期格式吗?

我的返回日期对象是包含的 "Mon Jul 12 00:00:00 IST 2010"

我正在尝试将此日期格式转换为"MM/dd/yyyy"但我得到解析异常.请帮我怎么转换它


OP评论中的代码:

String mydatObj = myDate.toString(); 
Date formatedDate = getDateFormat(mydatObj); 
public static Date getDateFormat(String dateString) { 
    Date date = null; 
    SimpleDateFormat format = new SimpleDateFormat("MM/dd/yyyy"); 
    try { 
        // set isLenient to false to adhere to the date format. 
        format.setLenient(false); 
        date = format.parse(dateString); 
    } catch (ParseException parseException) { 
        // ignore 
        LOG.error(parseException.getMessage(), parseException); 
    } 
    return date; 
}
Run Code Online (Sandbox Code Playgroud)

Yuv*_*dam 8

好吧,你得到的是ParseException因为你试图用错误的格式解析日期.

这是一个小代码片段,它将使用您拥有的格式:

// parse the date
DateFormat f = new SimpleDateFormat("E MMM dd HH:mm:ss zzz yyyy");
Date d = f.parse("Mon Jul 12 00:00:00 IST 2010"); // works

// now print the date
DateFormat out = new SimpleDateFormat("MM/dd/yyyy");
System.out.println(out.format(d));
Run Code Online (Sandbox Code Playgroud)