11

I have string form of date:

2011-03-27T09:39:01.607

and I want to format it to March 27, 2011

I am using

DateFormat[] formats = new DateFormat[] {
DateFormat.getDateInstance(), DateFormat.getDateTimeInstance(),
         DateFormat.getTimeInstance(), };
String actDate= formats[0].format(uploadeddate.substring(0,9));

but its not working.

How do I convert to March 27, 2011?

Willi Mentzel
  • 27,862
  • 20
  • 113
  • 121
Uday
  • 5,933
  • 9
  • 44
  • 76

9 Answers9

14

try this

SimpleDateFormat form = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS");
java.util.Date date = null;
try 
{
    date = form.parse("2011-03-27T09:39:01.607");
}
catch (ParseException e) 
{

    e.printStackTrace();
}
SimpleDateFormat postFormater = new SimpleDateFormat("MMMMM dd, yyyy");
String newDateStr = postFormater.format(date);

now newDateStr = March 27, 2011;

Willi Mentzel
  • 27,862
  • 20
  • 113
  • 121
Niranj Patel
  • 32,980
  • 10
  • 97
  • 133
9

May be this is of any help;

String convertDate(String inputDate) {

    DateFormat theDateFormat = new SimpleDateFormat("yyyy-MM-dd");
    Date date = null;

    try {
        date = theDateFormat.parse(inputDate);
    } catch (ParseException parseException) {
        // Date is invalid. Do what you want.
    } catch(Exception exception) {
        // Generic catch. Do what you want.
    }

    theDateFormat = new SimpleDateFormat("MMM dd, yyyy");

    return theDateFormat.format(date);
}
Mudassir
  • 13,031
  • 8
  • 59
  • 87
  • 1
    The format of date in first line, does not match with the format of input string. Anyways, this answer will also provide some help to the new comer. – Naved Sep 19 '11 at 05:41
4

You can use android.text.format.DateFormat in this way:

DateFormat.getMediumDateFormat(context).format(date);
Willi Mentzel
  • 27,862
  • 20
  • 113
  • 121
Luis Ollero
  • 383
  • 1
  • 2
  • 8
3
String dateimput = "2011-03-27T09:39:01.607"

SimpleDateFormat formatrecived = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");    
SimpleDateFormat formarwanted = new SimpleDateFormat("MMMM dd, yyyy");

Date recived = formatrecived.parse(dateimput);

Date output = formatwanted.format(recived);

Note: dateimput you need to format to yyyy-MM-dd hh:mm:ss using replace and substring

Niranj Patel
  • 32,980
  • 10
  • 97
  • 133
PedroAGSantos
  • 2,336
  • 2
  • 17
  • 34
1

To get AM or PM and 12 hour date format use hh:mm:ss a as string formatter where hh is for 12 hour format and a is for AM PM format.

note ## HH is for 24 hour and hh is for 12 hour date format.

SimpleDateFormat formatter = new SimpleDateFormat("mm/dd/yyyy hh:mm:ss a");
        String newFormat = formatter.format(testDate);

example

String date = "2011/11/12 16:05:06";
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy/mm/dd HH:MM:SS");
    Date testDate = null;
    try {
        testDate = sdf.parse(date);
    }catch(Exception ex){
        ex.printStackTrace();
    }
    SimpleDateFormat formatter = new SimpleDateFormat("mm/dd/yyyy hh:mm:ss a");
    String newFormat = formatter.format(testDate);
    System.out.println(".....Date..."+newFormat);
Willi Mentzel
  • 27,862
  • 20
  • 113
  • 121
John smith
  • 1,781
  • 17
  • 27
0

Example code using the Joda-Time library.

DateTime dateTime = new DateTime( "2011-03-27T09:39:01.607", DateTimeZone.forID( "Asia/Kolkata" ) );
DateTimeFormatter formatter = DateTimeFormat.forStyle( "M-" );
String output = formatter.withLocale( java.util.Locale.ENGLISH ).print( dateTime );
Willi Mentzel
  • 27,862
  • 20
  • 113
  • 121
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
0
public static String getFormatedDate(String strDate, String sourceFormate,
        String destinyFormate) {
    SimpleDateFormat df;
    df = new SimpleDateFormat(sourceFormate);
    Date date = null;
    try {
        date = df.parse(strDate);

    } catch (ParseException e) {
        e.printStackTrace();
    }

    df = new SimpleDateFormat(destinyFormate);
    return df.format(date);

}

Call it like:

getFormatedDate(strDate, "yyyy-MM-dd HH:mm:ss", "mm/dd/yyyy");
Willi Mentzel
  • 27,862
  • 20
  • 113
  • 121
0

I should like to contribute the modern answer.

java.time and ThreeTenABP

    DateTimeFormatter dateFormatter
            = DateTimeFormatter.ofLocalizedDate(FormatStyle.LONG);

    String uploadeddate = "2011-03-27T09:39:01.607";
    LocalDateTime ldt = LocalDateTime.parse(uploadeddate);
    String actDate = ldt.format(dateFormatter);
    System.out.println(actDate);

When I set my default locale to English and run this snippet, the output is what you asked for:

March 27, 2011

I am exploiting the fact that your string is in the standard ISO 8601 format, and that the classes of java.time parse this format as their default. Usually when reformatting a date/time string from one format to another, two formatters are used, one that specifies the format to convert from and one for the format to convert to. But since your source format is the default, we can make do with only one formatter here.

What went wrong in your code?

  • Your counting is wrong: uploadeddate.substring(0,9) gives you 2011-03-2 where I’m sure you intended 2011-03-27.
  • You passed a String to formats[0].format(), but it cannot format a string. It accepts either a Date or a Long, so you would have needed to convert to one of these first. The code compiles, but throws java.lang.IllegalArgumentException: Cannot format given Object as a Date.

Question: Can I use java.time on Android?

Yes, java.time works nicely on older and newer Android devices. It just requires at least Java 6.

  • In Java 8 and later and on newer Android devices (from API level 26) the modern API comes built-in.
  • In Java 6 and 7 get the ThreeTen Backport, the backport of the modern classes (ThreeTen for JSR 310; see the links at the bottom).
  • On (older) Android use the Android edition of ThreeTen Backport. It’s called ThreeTenABP. And make sure you import the date and time classes from org.threeten.bp with subpackages.

Links

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
0

What you'll want to do is create a format for your date to be able to parse the date out into the way you want it.

The formatted well have to be exactly the format of your timestamp and then what I did is create another format as to what I want the date to look like and then parse the it.

Check out the documentation for exactly what to use to format and then what to use to parse. Once I figured this out it got a lot easier.

Willi Mentzel
  • 27,862
  • 20
  • 113
  • 121
Pengume
  • 550
  • 11
  • 27