【问题标题】:Date and Time Formatting error [duplicate]日期和时间格式错误[重复]
【发布时间】:2016-08-05 09:34:37
【问题描述】:

我的日期是“2016-08-05 14:46:53 +05:30”,并且 我使用的日期和时间格式是“yyyy-MM-DD HH:mm:ss +05:30” 问题是,当我解析这个日期时,我得到输出 "Tue Jan 05 14:46:53 GMT+05:30 2016" 我不明白问题出在哪里。 我正在使用的代码发布在下面。

public class DateFormater {
private static String DATE_TIME_FORMAT = "yyyy-MM-DD HH:mm:ss +05:30";
private static String TAG = DateFormater.class.getSimpleName();
public static Date getDate(String s) {
    //Input s = 2016-08-05 14:46:53 +05:30
    Date date = null;
    try {
        SimpleDateFormat dateFormat=new SimpleDateFormat(DATE_TIME_FORMAT);
        date=dateFormat.parse(s);
    } catch (ParseException e) {
        e.printStackTrace();
    }
    return date;
}
public static String getCurrentDateString(Date date) {
    DateFormat dateFormat=SimpleDateFormat.getDateInstance(DateFormat.MEDIUM);
    Log.i(TAG, "getCurrentDateString: "+dateFormat.format(date));
    return dateFormat.format(date);
}
public static String getCurrentTimeString(Date date) {
    DateFormat dateFormat=SimpleDateFormat.getTimeInstance(DateFormat.SHORT);
    return dateFormat.format(date);//Tue Jan 05 14:46:53 GMT+05:30 2016
}}

【问题讨论】:

  • DD 不是您要查找的令牌,请使用 dd
  • 知道了,伙计。谢谢

标签: java date simpledateformat


【解决方案1】:

您的日期格式有问题。

public class DateFormater {
    private static String DATE_TIME_FORMAT = "yyyy-MM-dd HH:mm:ss +05:30";
    private static String TAG = DateFormater.class.getSimpleName();

    public static Date getDate(String s) {
        // Input s = 2016-08-05 14:46:53 +05:30
        Date date = null;
        try {
            SimpleDateFormat dateFormat = new SimpleDateFormat(DATE_TIME_FORMAT);
            date = dateFormat.parse(s);
            System.out.println(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }

    public static String getCurrentDateString(Date date) {
        DateFormat dateFormat = SimpleDateFormat
                .getDateInstance(DateFormat.MEDIUM);
        return dateFormat.format(date);
    }

    public static String getCurrentTimeString(Date date) {
        DateFormat dateFormat = SimpleDateFormat
                .getTimeInstance(DateFormat.SHORT);
        return dateFormat.format(date);// Tue Jan 05 14:46:53 GMT+05:30 2016
    }       
}

【讨论】:

    猜你喜欢
    • 2014-10-06
    • 1970-01-01
    • 2016-06-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-18
    相关资源
    最近更新 更多