35

我有一个如下值Mon Jun 18 00:00:00 IST 2012,我想将其转换为18/06/2012

如何转换这个?

我试过这个方法

public String toDate(Date date) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
        Date theDate = null;
        //String in = date + "/" + month + "/" + year;
        try {
            theDate = dateFormat.parse(date.toString());
            System.out.println("Date parsed = " + dateFormat.format(theDate));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return dateFormat.format(theDate);
    }

但它会引发以下异常:

java.text.ParseException: Unparseable date: "Mon Jun 18 00:00:00 IST 2012"

4

4 回答 4

81

我希望以下程序可以解决您的问题

String dateStr = "Mon Jun 18 00:00:00 IST 2012";
DateFormat formatter = new SimpleDateFormat("E MMM dd HH:mm:ss Z yyyy");
Date date = (Date)formatter.parse(dateStr);
System.out.println(date);        

Calendar cal = Calendar.getInstance();
cal.setTime(date);
String formatedDate = cal.get(Calendar.DATE) + "/" + (cal.get(Calendar.MONTH) + 1) + "/" +         cal.get(Calendar.YEAR);
System.out.println("formatedDate : " + formatedDate);    
于 2012-06-19T08:46:30.460 回答
8
于 2017-02-02T22:45:33.637 回答
6

只需添加: new SimpleDateFormat("bla bla bla", Locale.US)

public static void main(String[] args) throws ParseException {
    java.util.Date fecha = new java.util.Date("Mon Dec 15 00:00:00 CST 2014");
    DateFormat formatter = new SimpleDateFormat("EEE MMM dd HH:mm:ss Z yyyy", Locale.US);
    Date date;
    date = (Date)formatter.parse(fecha.toString());
    System.out.println(date);        

    Calendar cal = Calendar.getInstance();
    cal.setTime(date);
    String formatedDate = cal.get(Calendar.DATE) + "/" + 
            (cal.get(Calendar.MONTH) + 1) + 
            "/" +         cal.get(Calendar.YEAR);
    System.out.println("formatedDate : " + formatedDate);
}
于 2014-12-16T00:46:00.627 回答
0

您还可以在 excel 中使用公式,以便将此类日期转换为 excel 可以读取的日期类型: =DATEVALUE(CONCATENATE(MID(A1,8,3),MID(A1,4,4),RIGHT(A1,4)))

你得到:12/7/2016从:Wed Dec 07 00:00:00 UTC 2016

于 2017-02-02T12:47:47.277 回答