0

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.

Rohit Patil
  • 103
  • 1
  • 7
  • 1
    Try to use `DateTimeFormatter`: https://stackoverflow.com/questions/22463062/how-to-parse-format-dates-with-localdatetime-java-8 – Seldo97 Jul 28 '20 at 17:44

1 Answers1

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"));
}
vikas kumar
  • 10,447
  • 2
  • 46
  • 52