0

I have a CSV file with a date string column. I want to spend the CSV data into mysql.

In CSV has a String: "Sun Oct 05 20:59:57 BRT 2014" and I want to convert to DateTime.

How do I do this?

Roger Rubens
  • 163
  • 1
  • 1
  • 9
  • You referring to Joda [`DateTime`](http://joda-time.sourceforge.net/apidocs/org/joda/time/DateTime.html) or Java 8 [`LocalDateTime`](https://docs.oracle.com/javase/8/docs/api/java/time/LocalDateTime.html)? – Andreas Aug 29 '15 at 15:09
  • I have a csv file with a date string column. I want to spend the csv data into mysql. – Roger Rubens Aug 29 '15 at 15:12
  • Your question says `DateTime`, but Java doesn't have a built-in class with the name. There is a class with that name in the [Joda Time](http://www.joda.org/joda-time/) library. Did you mean to say "DateTime", or are you asking about the built-in `java.util.Date`, or the newer `java.time.LocalDateTime`? – Andreas Aug 29 '15 at 15:18
  • in mysql has the datetime type, I just need to pass the string to the format compatible. – Roger Rubens Aug 29 '15 at 15:22
  • So you want to parse the given string from the CSV file into a `java.sql.Timestamp` so it can be used on a `PreparedStatement.setTimestamp()` call, right? --- Please elaborate question. – Andreas Aug 29 '15 at 15:25
  • @RogerRubens Check my answer, let me know it it's what you want – Skizo-ozᴉʞS ツ Aug 29 '15 at 15:28

3 Answers3

2

You should use this 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

Community
  • 1
  • 1
Skizo-ozᴉʞS ツ
  • 19,464
  • 18
  • 81
  • 148
0

You should use this code:

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

DateTime dt = formatter.parseDateTime(string);
Epic
  • 113
  • 1
  • 12
Rahman
  • 3,755
  • 3
  • 26
  • 43
0

Try DateTimeFormat

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

Here is similar question.

Community
  • 1
  • 1
urajah
  • 193
  • 2
  • 7