Sanat Pandey
Sanat Pandey

Reputation: 4103

Convert DateString in a particular format

I have a problem that I have Date String in the format "2011-09-28T10:33:53.694+0000". I want to change this Date format in MM/DD/YYYY, don't know How? Please suggest me for right result.

Upvotes: 1

Views: 153

Answers (3)

Caner
Caner

Reputation: 59198

String string = "2011-09-28T10:33:53.694+0000";
SimpleDateFormat inFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ", Locale.ENGLISH);
SimpleDateFormat outFormat = new SimpleDateFormat("MM/dd/yyyy", Locale.ENGLISH);
Date date;
String output;
try {
    date = inFormat.parse(string);
    output = outFormat.format(date); // 28/09/2011
} catch (ParseException e) {
    e.printStackTrace();
}

Upvotes: 0

Rocker
Rocker

Reputation: 681

Do it this way ---->

  1. Create object Date class Date date = new Date();
  2. Create object of Simple Date Format class SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy"); Note -> MM - will give month in character and dd and yyyy will give date and year in numeric
  3. Convert date into string String s = sdf.format(date);

and you are done with it...

Upvotes: 0

duffymo
duffymo

Reputation: 308793

Something like this. Check the details for your "from" format.

DateFormat from = new SimpleDateFormat("yyyy-MM-ddThh:mm:ss");  // not sure about the details
from.setLenient(false);
DateFormat to = new SimpleDateFormat("MM/dd/yyyy");
to.setLenient(false);
Date d = from.parse(dateStr);
System.out.println(to.format(d));

Upvotes: 1

Related Questions