简体   繁体   中英

Date Format Conversion in Java?

I have a date coming in JSON feeds in the format below:

 mm/dd/yyyy  for example today's date  10/26/2011

I want to convert it to another format, which is, infact a detailed date format as:

 Wed, 26 OCT 2011

How can i do that..??? I know this could be very simple, but am still a newbie, Any help is appreciated.

Here is an example where I do exactly that in my android app.

public String GetJustTime(String input){
    SimpleDateFormat curFormater = new SimpleDateFormat("YYYY-MM-dd HH:mm:ss");
    Date dateObj = new Date();
        try {
            dateObj = curFormater.parse(input);
        } catch (ParseException e) {}
        SimpleDateFormat postFormater = new SimpleDateFormat("h:mm a");
        String newDateStr = postFormater.format(dateObj);

    return newDateStr;
}

Just change the 2 format string as appropriate for your needs. The First one is for the incoming format, and the 2nd one is the format used for the output.

I beleive the output format you want is "EEE, d MMM YYYY", but I can't test it right now.

It's not very educational, but here's the solution ready to handle your request:

String dateString = "10/26/2011";
String dateStringParsed = "";
SimpleDateFormat format1 = new SimpleDateFormat("MM/dd/yyyy");
SimpleDateFormat format2 = new SimpleDateFormat("EEE, dd MMM yyyy");

try {
    Date parsed = format1.parse(dateString);
    dateStringParsed = format2.format(parsed);
}
catch(ParseException pe) { 
    //handle the exception
}

AFAIK, capitals in month are not available via SimpleDateFormat. You're gonna have to change it manually if it's necessary.

In addition to the solutions with SimpleDateFormat, just wanted to add an alternative solution with Joda's DateTimeFormatter ( org.joda.time.format.DateTimeFormatter ), which is generally considered a much better date/time API if you end up doing anything the least bit advanced with dates or times in your code. This solution can actually be all inlined into one line, but showing broken out here for clarity:

String source = "10/26/2011";
String target = "Wed, 26 Oct 2011";

DateTimeFormatter sourceFormatter = DateTimeFormat.forPattern("MM/dd/yyyy");
DateTimeFormatter targetFormatter = DateTimeFormat.forPattern("E, dd MMM yyyy");

assertEquals(target, sourceFormatter.parseDateTime(source).toString(targetFormatter));

You can take a look at the SimpleDateFormat Class. One small example on how you can use this, taken from here :

String dateString = new String("07/12/2005");
java.util.Date dtDate = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("dd-MMM-yy");
SimpleDateFormat sdfAct = new SimpleDateFormat("dd/mm/yyyy");
try
{
  dtDate = sdfAct.parse(dateString);
  System.out.println("Date After parsing in required format:"+(sdf.format(dtDate)));
}
catch (ParseException e)
{
  System.out.println("Unable to parse the date string");
  e.printStackTrace();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM