-2

我想在android中将时间戳转换为日期,我尝试了这段代码但没有得到预期的结果。

我的代码是。

String timeStampST = "2016-12-13 09:47:11";
SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy");
Date convertedDate = new Date();
try {
    convertedDate = dateFormat.parse(timeStampST);
    } catch (ParseException e) {
    e.printStackTrace();
}

System.out.println(convertedDate);

输出

Tue Dec 13 16:15:02 GMT+05:30 2016

预期输出

Tue Dec 13 2016

我的输出需要一天

4

2 回答 2

3

请复制粘贴此答案。它会帮助你。

        String timeStampST = "2016-12-13 09:47:11";
        SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = null;
        String resultDate = null;
        try {
            date = fmt.parse(timeStampST);
            SimpleDateFormat fmtOut = new SimpleDateFormat("EEE MMM dd yyyy");
            resultDate = fmtOut.format(date);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

输出

结果日期 = 2016 年 12 月 13 日

于 2016-12-13T10:50:12.290 回答
1

将此方法用于字符串和输入输出格式。

  public static String parseDateToddMMyyyy(String time, String inputPattern, String outputPattern) {
    SimpleDateFormat inputFormat = new SimpleDateFormat(inputPattern);
    SimpleDateFormat outputFormat = new SimpleDateFormat(outputPattern);
    Date date;
    String str = null;
    try {
        date = inputFormat.parse(time);
        str = outputFormat.format(date);
    } catch (ParseException e) {
        e.printStackTrace();
    }
    return str;
}
于 2016-12-13T10:48:01.857 回答