将字符串转换为时间戳Java

时间:2015-06-23 14:48:26

标签: java timestamp parseexception

所以我遇到了这个问题,我无法解决这个问题。我已经阅读了类似的问题,但事实上我发现格式存在问题,而且格式正确。

基本上我正在尝试将String转换为时间戳,并且我得到了不可解析的日期错误。

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Hello {

public static Timestamp convertStringToTimestamp(String str_date) {
    try {

        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss,SSS");
        Date date = formatter.parse(str_date);
        java.sql.Timestamp timeStampDate = new Timestamp(date.getTime());
        return timeStampDate;

    } catch (ParseException e) {
        System.out.println("Exception :" + e);
        return null;
    }
}

public static void main(String[] args) {
    Timestamp ts = convertStringToTimestamp("2015-06-09 11:51:12,708");
    Timestamp ts2 = convertStringToTimestamp("2015-04-17 11:29:49.564");
    System.out.println(ts +" | "+ts2);


}

}

输出:

Exception :java.text.ParseException: Unparseable date: "2015-06-09 11:51:12,708"
Exception :java.text.ParseException: Unparseable date: "2015-04-17 11:29:49.564"
null | null

有什么想法吗?

4 个答案:

答案 0 :(得分:0)

这对我来说很完美。

我刚刚传递了正确的模式作为输入

public static Timestamp convertStringToTimestamp(String str_date, String pattern) {
        try {

            SimpleDateFormat formatter = new SimpleDateFormat(pattern);
            Date date = formatter.parse(str_date);
            java.sql.Timestamp timeStampDate = new Timestamp(date.getTime());
            return timeStampDate;

        } catch (ParseException e) {
            System.out.println("Exception :" + e);
            return null;
        }
    }

    public static void main(String[] args) {
        Timestamp ts = convertStringToTimestamp("2015-06-09 11:51:12,708", "yyyy-MM-dd HH:mm:ss,SSS");
        Timestamp ts2 = convertStringToTimestamp("2015-04-17 11:29:49.564", "yyyy-MM-dd HH:mm:ss.SSS");
        System.out.println(ts +" | "+ts2);


    }

输出结果为:

2015-06-09 11:51:12.708 | 2015-04-17 11:29:49.564

答案 1 :(得分:0)

" 2015-06-09 11:51:12,708"正在为我工​​作但是" 2015-04-17 11:29:49.564"韩元'吨。您为","指定了正则表达式所以"。"不会。这是完全正常的。

答案 2 :(得分:0)

你需要修复逗号

Timestamp ts2 = convertStringToTimestamp("2015-04-17 11:29:49.564");

答案 3 :(得分:-2)

**Update**
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;


public class SomeClass {

    public static void main(String[] args) {


         System.out.println(convertStringToTimestamp("2015-06-09 11:51:12,708"));
         //be consistent here with , and .
         System.out.println(convertStringToTimestamp("2015-04-17 11:29:49.564"));
         System.out.println();


    }

    private static Timestamp  convertStringToTimestamp(String something) {


SimpleDateFormat dateFormat = null;
        if(something.contains(".")) {
            dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss.SSS");
        } 
        if(something.contains(",")) {
            dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss,SSS");
        }
        Timestamp timestamp = null;
            Date parsedDate;
            try {
                parsedDate = dateFormat.parse(something);
                 timestamp = new java.sql.Timestamp(parsedDate.getTime());

            } catch (ParseException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            return timestamp;
    }



}