如何将字符串时间戳解析为Java日期,格式为Fri Jul 17 12:41:20 IST 2020?

huangapple go评论77阅读模式
英文:

How to read string timestamp into Java date of form Fri Jul 17 12:41:20 IST 2020?

问题

我需要将上述字符串转换为日期型 POJO 以便将其保存在 MS SQL 服务器表中,列类型为 DATETIME。

英文:

I have a string in the form of Fri Jul 17 12:41:20 IST 2020 and I want to persist this in MS SQL server table with column type DATETIME.

For that, I first need to convert the above string into Date POJO.

答案1

得分: 1

You can also store long if needed but there is a basic conversion on string to Date or Long.

 //Fri Jul 17 12:41:20 IST 2020
public static Long getDateTime(String input){
    SimpleDateFormat simpleDateFormat =  new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy");
    try {
        Date date = simpleDateFormat.parse(input);
        return date.getTime();
    } catch (ParseException e) {
        e.printStackTrace();
        return null;
    }
}

public static void main(String[] args){
    System.out.println(DateConverter.getDateTime("Fri Jul 17 12:41:20 IST 2020"));
}
英文:

You can also store long if needed but there is a basic conversion on string to Date or Long.

 //Fri Jul 17 12:41:20 IST 2020
public static Long getDateTime(String input){
    SimpleDateFormat simpleDateFormat =  new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy");
    try {
        Date date = simpleDateFormat.parse(input);
        return date.getTime();
    } catch (ParseException e) {
        e.printStackTrace();
        return null;
    }
}

public static void main(String[] args){
    System.out.println(DateConverter.getDateTime("Fri Jul 17 12:41:20 IST 2020"));
}

huangapple
  • 本文由 发表于 2020年7月29日 01:35:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/63139785.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定