-2

I have String as "Nov 12 2014 10:28AM". I want to convert this string into DateTime format. how can I convert that column into DateTime?

MysticMagicϡ
  • 28,593
  • 16
  • 73
  • 124
vish
  • 45
  • 1
  • 8
  • i tried String dateBef = sdf.format(bs.LVP_DateTime); Date LVPDate = new Date(dateBef); – vish Nov 12 '14 at 05:06
  • possible duplicate of [Java string to date conversion](http://stackoverflow.com/questions/4216745/java-string-to-date-conversion) – codePG Nov 12 '14 at 05:07
  • Do search in Google there are many solutions found......pick up anyone..... – M D Nov 12 '14 at 05:08

3 Answers3

1

You can use SimpleDateFormat for it:

String dateString = "Nov 12 2014 10:28AM";
SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss aa");  //change this format as per your need
Date myDate = new Date();
try {
    myDate = dateFormat.parse(dateString);
} catch (ParseException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

Hope it helps.

MysticMagicϡ
  • 28,593
  • 16
  • 73
  • 124
  • java.text.ParseException: Unparseable date: "Nov 12 2014 10:47AM" (at offset 6) – vish Nov 12 '14 at 05:18
  • That's probably issue in dateString. you can try with `String dateString = "Nov 12, 2014 10:28AM";` if Naveen's answer doesn't work @user3256678 – MysticMagicϡ Nov 12 '14 at 05:21
0
   String string = "Nov 12 2014 10:28AM";
    Date date = null;
    try {
        date = new SimpleDateFormat("MMMM d yyyy hh:mmaa", Locale.ENGLISH).parse(string);
    } catch (ParseException e) {
        e.printStackTrace();
    }
    System.out.println(date); // Wed Nov 12 10:28:00 IST 2014
Naveen Tamrakar
  • 3,349
  • 1
  • 19
  • 28
0

Here is how you can do :

 public static final String DATE_TIME_FORMAT = "yyyy-MM-dd HH:mm:ss";
 // you can use the format type you want

 public static Date stringDateToDate(String StrDate) {
    Date dateToReturn = null;
    SimpleDateFormat dateFormat = new SimpleDateFormat(DATE_TIME_FORMAT);

    try {
        dateToReturn = (Date) dateFormat.parse(StrDate);
    } catch (ParseException e) {
        e.printStackTrace();
    }

    return dateToReturn;
}

Hope it helps :)

Syed Raza Mehdi
  • 4,067
  • 1
  • 31
  • 47