Home » Android » java – Date format conversion Android

java – Date format conversion Android

Posted by: admin May 14, 2020 Leave a comment

Questions:

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?

How to&Answers:

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;

Answer:

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);
}

Answer:

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

DateFormat.getMediumDateFormat(context).format(date);

Answer:

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

Answer:

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);

Answer:

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");

Answer:

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 );

Answer:

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.

Answer:

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