【问题标题】:how to parse a formatted string date to date object in java [duplicate]如何在java中解析格式化的字符串日期到日期对象[重复]
【发布时间】:2016-02-24 14:23:36
【问题描述】:

我试图解析格式化的字符串日期,但出现解析错误
输入日期为“Wed Nov 11 14:24:46 IST 2015”,需要输出日期为“Wed Nov 11 2015 14:24:46 IST”

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class DateFormat {
    public static void main(String[] args) {
        try {
            String target = "Wed Nov 11 14:24:46 IST 2015";
            SimpleDateFormat df = new SimpleDateFormat("EEE MMM dd kk:mm:ss zzz yyyy");
            Date result =  df.parse(target);

            SimpleDateFormat df2 = new SimpleDateFormat("EEE MMM dd yyyy kk:mm:ss zzz");
            String result2 =  df2.format(result);

            Date result3 =  df.parse(result2);
            System.out.println(result2);
            System.out.println(result3);
        } catch (ParseException pe) {
            pe.printStackTrace();
        }
    }
}

出现 java.text.ParseException 错误:无法解析的日期:“Wed Nov 11 2015 14:24:46 IST”

【问题讨论】:

标签: java date date-format simpledateformat


【解决方案1】:

我已更新我的答案以进行您的问题/评论中提到的解析。请看下面的解释:

"Wed Nov 11 14:24:46 IST 2015"

到下面

"Wed Nov 11 2015 14:24:46 IST"

我设置了两个 SimpleDateFormat 对象如下

SimpleDateFormat sourceFormat, destinationFormat;

//this is to format the string to Date object
sourceFormat = new SimpleDateFormat("EEE MMM d kk:mm:ss zzz yyyy", Locale.US);
//this is to format the Date object to desired pattern
destinationFormat = new SimpleDateFormat("EEE MMM d yyyy kk:mm:ss zzz", Locale.US);

然后我将时区设置如下

TimeZone istTimeZone = TimeZone.getTimeZone("Asia/Kolkata");

sourceFormat.setTimeZone(istTimeZone);
destinationFormat.setTimeZone(istTimeZone);

我使用 sourceFormat 对象将日期字符串格式化为 Date 对象,如下所示:

Date sourceDate = sourceFormat.parse(target);
//output: Wed Nov 11 08:54:46 GMT 2015

然后我使用目标格式来格式化代表字符串的日期对象,如下所示:

Date destinationDate = destinationFormat.format(d);
//output: Wed Nov 11 2015 14:24:46 IST

基本上,为了获得合法的 Date 对象,我必须使用第一个 SimpleDateFormat sourceFormat,其中包含映射字符串中日期的模式。一旦使用 String 创建了合法的 Date 对象,我就使用第二个格式化程序重新格式化 Date 对象。下面是完整的代码,如果复制/粘贴应该给出输出。

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;

public class ParseDate {
    public static void main(String[] args) {
        try {
            String target = "Wed Nov 11 14:24:46 IST 2015";
            SimpleDateFormat sourceFormat, destinationFormat;

            sourceFormat = new SimpleDateFormat("EEE MMM d kk:mm:ss zzz yyyy", Locale.US);
            destinationFormat = new SimpleDateFormat("EEE MMM d yyyy kk:mm:ss zzz", Locale.US);

            TimeZone istTimeZone = TimeZone.getTimeZone("Asia/Kolkata");

            sourceFormat.setTimeZone(istTimeZone);
            destinationFormat.setTimeZone(istTimeZone);

            Date d = sourceFormat.parse(target);
            System.out.println(d.toString());
            //output: Wed Nov 11 08:54:46 GMT 2015

            System.out.println(destinationFormat.format(d));
            //output: Wed Nov 11 2015 14:24:46 IST

        } catch (ParseException pe) {
            pe.printStackTrace();
        }
    }
} 

【讨论】:

  • 目标变量需要解析格式为"EEE MMM dd yyyy kk:mm:ss zzz""
  • @Brittas 现在怎么样?它解决了问题的所有要求。
  • 仅供参考,非常麻烦的旧日期时间类,例如 java.util.Datejava.util.Calendarjava.text.SimpleDateFormat,现在是 legacy,被 Java 8 中内置的 java.time 类所取代,之后。见Tutorial by Oracle
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-06-23
  • 2011-08-27
  • 2012-06-27
  • 2012-01-07
  • 2016-05-17
相关资源
最近更新 更多