如何转换“星期一6月18 00:00:00 IST 2012”到18/06/2012? [英] How to convert "Mon Jun 18 00:00:00 IST 2012" to 18/06/2012?

查看:188
本文介绍了如何转换“星期一6月18 00:00:00 IST 2012”到18/06/2012?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个像以下这样的价值 Mon Jun 18 00:00:00 IST 2012 ,我想转换成 18/06 / 2012

I have a value like the following Mon Jun 18 00:00:00 IST 2012 and I want to convert this to 18/06/2012

如何转换?

我尝试过这种方法>

I tried this method

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:不可稀释的日期:星期一6月18 00:00:00 IST 2012

推荐答案

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

I hope following program will solve your problem

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);    

这篇关于如何转换“星期一6月18 00:00:00 IST 2012”到18/06/2012?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆