如何将"Mon Jun 18 00:00:00 IST 2012"更改为2012年6月18日?

SAR*_*SAR 29 java date

我有一个类似下面的值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);
    }
Run Code Online (Sandbox Code Playgroud)

但它抛出以下异常:

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

Rah*_*wal 64

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

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);    
Run Code Online (Sandbox Code Playgroud)

  • 我认为_dateStr_示例的格式应为:SimpleDateFormat(“ ** EEE MMM dd HH:mm:ss z yyyy **”) (2认同)

Bas*_*que 7

时间

现代方法是使用java.time类。这些取代麻烦的旧的遗留日期时间类,如DateCalendarSimpleDateFormat

解析为ZonedDateTime.

String input = "Mon Jun 18 00:00:00 IST 2012";
DateTimeFormatter f = DateTimeFormatter.ofPattern( "E MMM dd HH:mm:ss z uuuu" )
                                       .withLocale( Locale.US );
ZonedDateTime zdt = ZonedDateTime.parse( input , f );
Run Code Online (Sandbox Code Playgroud)

提取一个只有日期的对象 a LocalDate,没有任何时间和时区。

LocalDate ld = zdt.toLocalDate();
DateTimeFormatter fLocalDate = DateTimeFormatter.ofPattern( "dd/MM/uuuu" );
String output = ld.format( fLocalDate) ;
Run Code Online (Sandbox Code Playgroud)

转储到控制台。

System.out.println( "input: " + input );
System.out.println( "zdt: " + zdt );
System.out.println( "ld: " + ld );
System.out.println( "output: " + output );
Run Code Online (Sandbox Code Playgroud)

输入:星期一 6 月 18 日 00:00:00 IST 2012

zdt: 2012-06-18T00:00+03:00[亚洲/耶路撒冷]

ld: 2012-06-18

输出:18/06/2012

查看此代码在 IdeOne.com 中实时运行

格式选择不当

您的格式是数据交换的糟糕选择:人类难以阅读,计算机难以解析,使用非标准的 3-4 个字母区域代码,并假设为英语。

而是尽可能使用标准的ISO 8601格式。java.time 类在解析/生成日期时间值时默认使用ISO 8601格式。

以、、 或等格式指定正确的时区名称。永远不要使用 3-4 个字母的缩写,例如或因为它们不是真正的时区,不是标准化的,甚至不是唯一的(!)。例如,您使用的可能是爱尔兰标准时间、以色列标准时间(由 java.time 解释,见上文)或印度标准时间。continent/regionAmerica/MontrealAfrica/CasablancaPacific/AucklandESTISTIST


关于 java.time

java.time框架是建立在Java 8和更高版本。这些类取代了麻烦的旧的遗留日期时间类,例如java.util.Date, Calendar, & SimpleDateFormat

现在处于维护模式Joda-Time项目建议迁移到java.time类。

要了解更多信息,请参阅Oracle 教程。并在 Stack Overflow 上搜索许多示例和解释。规范是JSR 310

从哪里获得 java.time 类?

ThreeTen-额外项目与其他类扩展java.time。该项目是未来可能添加到 java.time 的试验场。你可能在这里找到一些有用的类,比如IntervalYearWeekYearQuarter,和更多


小智 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);
}
Run Code Online (Sandbox Code Playgroud)