简体   繁体   English

如何在Java中将Date字符串转换为DateTime?

[英]How to convert Date String to DateTime in Java?

I have a CSV file with a date string column. 我有一个带有日期字符串列的CSV文件。 I want to spend the CSV data into mysql. 我想将CSV数据花费到mysql中。

In CSV has a String : "Sun Oct 05 20:59:57 BRT 2014" and I want to convert to DateTime . 在CSV中有一个String“ Sun Oct 05 20:59:57 BRT 2014” ,我想转换为DateTime

How do I do this? 我该怎么做呢?

You should use this DateTimeFormat 您应该使用此DateTimeFormat

EEE MMM dd HH:mm:ss z yyyy

And now you should do this : 现在,您应该这样做:

String string = "Sun Oct 05 20:59:57 BRT 2014";
DateTimeFormatter formatter = DateTimeFormat.forPattern("EEE MMM dd HH:mm:ss z yyyy");
Date dt = formatter.parse(string);         //if you want to use DateTime do it DateTime dt = formatter.parse(string);
System.out.println(" Date " + datetime.toString());

EDIT 编辑

First of all include those imports: 首先包括那些进口:

import java.sql.Timestamp;
import java.text.SimpleDateFormat;

Then your code should looks like as follows : 然后您的代码应如下所示:

String string = "Sun Oct 05 20:59:57 BRT 2014";
SimpleDateFormat formatter = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy");
java.sql.Timestamp datetime = new Timestamp(formatter.parse(string).getTime());
System.out.println("DateTime: " + datetime.toString());

To use DateTime you should check this Question 要使用DateTime您应该检查此问题

You should use this code: 您应该使用以下代码:

DateTimeFormatter formatter = DateTimeFormat.forPattern("dd/MM/yyyy   HH:mm:ss");

DateTime dt = formatter.parseDateTime(string);

Try DateTimeFormat 试试DateTimeFormat

DateTimeFormatter formatter = DateTimeFormat.forPattern("EEE MMM dd HH:mm:ss z yyyy");
DateTime dt = formatter.parseDateTime(string);

Here is similar question. 是类似的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM