0

how to convert string [] temp to Timestamp:

    String[] temp = line.split(",");
            for (int i = 1; i < temp.length; i++) {

                    objekt.setTimestamp(timestamp);}

if i use

Timestamp ts= Timestamp.valueOf(temp[0]);
objekt.setTimestamp(ts);}

i get this exceptionTimestamp format must be yyyy-mm-dd hh:mm:ss[.fffffffff]

if i use this

SimpleDateFormat dateFormat = new SimpleDateFormat("MM-dd-yyyy hh:mm:ss");
                java.util.Date parsedDate = dateFormat.parse(temp[0]);
                java.sql.Timestamp timestamp = new java.sql.Timestamp(parsedDate.getTime());

i get " java.text.ParseException: Unparseable date: "06/11/2013 22:00"

thank you everyone for suggestions, i can read the timestamp but i must to read this format month-day-year: 06/18/2013 21:00:00, and with SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy HH:mm"); i get 2013-06-18 18:00:00, if i change MM or dd or yyyy hier "MM/dd/yyyy HH:mm" i can't even get the month first then date and year

user3721625
  • 267
  • 1
  • 3
  • 9
  • FYI, the troublesome old date-time classes such as [`java.util.Date`](https://docs.oracle.com/javase/9/docs/api/java/util/Date.html), [`java.util.Calendar`](https://docs.oracle.com/javase/9/docs/api/java/util/Calendar.html), and `java.text.SimpleDateFormat` are now [legacy](https://en.wikipedia.org/wiki/Legacy_system), supplanted by the [java.time](https://docs.oracle.com/javase/9/docs/api/java/time/package-summary.html) classes built into Java 8 & Java 9. See [Tutorial by Oracle](https://docs.oracle.com/javase/tutorial/datetime/TOC.html). – Basil Bourque Feb 11 '18 at 02:17

4 Answers4

3

Your date format must actually match the text being provided. '/' and '-' do not match.

For the string you have provided, it appear the format might be "MM/dd/yyyy HH:mm".

It could also be "dd/MM/yyyy HH:mm", as it is not clear whether that date is June 11 or November 6.

user3145373 ツ
  • 7,858
  • 6
  • 43
  • 62
Brett Okken
  • 6,210
  • 1
  • 19
  • 25
2

Use the below code:

SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy HH:mm");

Assuming that 06 in the string Unparseable date: "06/11/2013 22:00" correponds to month

Dinal
  • 661
  • 4
  • 9
2

On your 2nd code block where you got java.text.ParseException: Unparseable date: "06/11/2013 22:00, your SimpleDateFormat pattern is wrong as your String is "06/11/2013 22:00", So you have to use pattern :

SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy HH:mm");

For more details, see java.text.SimpleDateFormat

earthmover
  • 4,395
  • 10
  • 43
  • 74
1

You can do something like this :

Timestamp ts= null;
SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy");
String[] temp = {..,..}; // it's string time array
        for (int i = 1; i < temp.length; i++) {            
            Date convertedDate = dateFormat.parse(temp[i]);
            ts = new Timestamp(convertedDate.getTime());
            System.out.println(ts);
         }
user3145373 ツ
  • 7,858
  • 6
  • 43
  • 62