-4

I have string like below and I need to convert this as a date in java

"Tue Sep 30 00:00:00 GMT+05:30 2014"

This is a string representation of the java date object. I get it as a string where I need to parse it like "30 Sep 2014"

I thought if I could get this as a date object I can get whatever format I want using simpledateformat. But the problem is how do I get the date Object from this string

Gautam Savaliya
  • 1,403
  • 2
  • 20
  • 31
seeker
  • 77
  • 1
  • 11

4 Answers4

1

The format you're looking for is this:

SimpleDateFormat format = new SimpleDateFormat("EEE MMM dd hh:mm:ss z yyyy");

You can check all the valid formats here http://docs.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html

ares
  • 4,283
  • 6
  • 32
  • 63
1

You can also use SimpleDateFomrat to parse the string into date:

String s = "Tue Sep 30 00:00:00 GMT+05:30 2014";
SimpleDateFormat df = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy", Locale.US);
System.out.println(df.parse(s));
Jens
  • 67,715
  • 15
  • 98
  • 113
0
public static Date convertDate(final String dateValue) {
    final SimpleDateFormat format = new SimpleDateFormat("dd MMM yyyy");
    Date date = null;
    try {
        date = format.parse(dateValue);
        return date;
    } catch (final ParseException e) {
        LOG.error(e.getMessage(), e);
        return null;
    }
}
phani sekhar
  • 107
  • 1
  • 1
  • 13
-1

the java 8 version... and you will find much more entries already exist in stackoverflow.

Community
  • 1
  • 1
user2504380
  • 535
  • 4
  • 12