英文:
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"));
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论