Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change date string format in android

Tags:

android

I am getting date string from SAX parsing like this: Wed, 18 Apr 2012 07:55:29 +0000

Now, I want this string as : Apr 18, 2012 01:25 PM

How can I do this?

like image 890
Krishna Suthar Avatar asked May 03 '12 06:05

Krishna Suthar


People also ask

How can I change the date format in Android?

First Create a Calendar object using your Date object. Then build a String using date, year, month and etc you need. then you can use it. You can get data using get() method in Calendar class.

How do I convert a date to a specific format?

Convert date to different format with Format CellsSelect the dates you want to convert, right click to select Format Cells from context menu. 2. In the Format Cells dialog, under Number tab, select Date from Category list, and then select one format you want to convert to from the right section.

How do I change the date format in kotlin Android?

yyyy HH:mm"); String output = formatter. format(localDateTime); If this does not work with api21, you can use: SimpleDateFormat parser = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss"); SimpleDateFormat formatter = new SimpleDateFormat("dd.


3 Answers

SimpleDateFormat format = new SimpleDateFormat("MMM dd,yyyy  hh:mm a");
String date = format.format(Date.parse("Your date string"));

UPDATE :-

As on, Date.parse("Your date string"); is deprecated.

String strCurrentDate = "Wed, 18 Apr 2012 07:55:29 +0000";
SimpleDateFormat format = new SimpleDateFormat("EEE, dd MMM yyyy hh:mm:ss Z");
Date newDate = format.parse(strCurrentDate);

format = new SimpleDateFormat("MMM dd,yyyy hh:mm a");
String date = format.format(newDate);
like image 70
V.J. Avatar answered Sep 30 '22 16:09

V.J.


This will do it:

public static String formateDateFromstring(String inputFormat, String outputFormat, String inputDate){

    Date parsed = null;
    String outputDate = "";

    SimpleDateFormat df_input = new SimpleDateFormat(inputFormat, java.util.Locale.getDefault());
    SimpleDateFormat df_output = new SimpleDateFormat(outputFormat, java.util.Locale.getDefault());

    try {
        parsed = df_input.parse(inputDate);
        outputDate = df_output.format(parsed);

    } catch (ParseException e) { 
        LOGE(TAG, "ParseException - dateFormat");
    }

    return outputDate;

}

Example:

String date_before = "1970-01-01";
String date_after = formateDateFromstring("yyyy-MM-dd", "dd, MMM yyyy", date_before);

Output:

date_after = "01, Jan 1970";
like image 20
elirigobeli Avatar answered Sep 30 '22 16:09

elirigobeli


From oficial documentation, in the new API (18+) you should be implement this:

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS", Locale.US);
String time=sdf.format(new Date());

Documentation: http://developer.android.com/reference/java/text/SimpleDateFormat.html

like image 6
Hpsaturn Avatar answered Sep 30 '22 15:09

Hpsaturn