41

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?

Krishna Suthar
  • 3,071
  • 6
  • 31
  • 37

8 Answers8

146
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);
V.J.
  • 9,492
  • 4
  • 33
  • 49
20

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";
elirigobeli
  • 1,391
  • 2
  • 15
  • 22
6

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

Hpsaturn
  • 2,702
  • 31
  • 38
3

This is works for me, using SimpleDateFormat and Calendar

String originDate = "Wed, 18 Apr 2012 07:55:29 +0000";
SimpleDateFormat formatIn = new SimpleDateFormat("EEE, dd MMM yyyy hh:mm:ss Z");
SimpleDateFormat formatOut = new SimpleDateFormat("MMM dd,yyyy hh:mm a");
Calendar calendar = Calendar.getInstance();
calendar.setTime(formatIn.parse(originDate));

String newDate = formatOut.format(calendar.getTime());
areg_noid
  • 81
  • 4
2

The easiest solution is to use DateFormat's getter methods: The getDateTimeInstance DateFormat object formats date like that: Dec 31, 1969 4:00:00 PM, however there are those extra zeroes after 4:00

Date date = new Date();
String fDate = DateFormat.getDateTimeInstance().format(date);
System.out.println(fDate);
nrallakis
  • 161
  • 11
1

java.time and ThreeTenABP

The modern answer to this question is overdue. When this question was asked in 2012, using SimpleDateFormat and/or DateFormat as the other answers do, was right even though those classes had always been troublesome. They were replaced just a couple of years later by java.time, the modern Java date and time API, which I frankly find much nicer to work with. So so I am doing.

I suggest you separate your conversion into two operations. In you program don’t keep date and time as a string. Keep them as a proper date-time object. So when parsing from SAX also parse the date-time string into an OffsetDateTime immediately. When at a later point you need to show the date and time to the user (or transmit it to another system), convert it into the user’s time zone and format it into an appropriate string for that purpose.

Parse into an OffsetDateTime

The string you got conforms with RFC 1123 format. java.time has a built-in formatter for that, which is good because it saves us from constructing our own formatter.

    String stringFromSaxParsing = "Wed, 18 Apr 2012 07:55:29 +0000";

    OffsetDateTime dateTime = OffsetDateTime.parse(
            stringFromSaxParsing, DateTimeFormatter.RFC_1123_DATE_TIME);

    System.out.println(dateTime);

Output from this snippet is:

2012-04-18T07:55:29Z

Convert to user’s time zone and format

    DateTimeFormatter wantedFormatter = DateTimeFormatter.ofPattern("MMM dd, uuuu hh:mm a", Locale.ENGLISH);

    String formatted = dateTime.atZoneSameInstant(ZoneId.systemDefault())
            .format(wantedFormatter);

    System.out.println(formatted);

I ran this in Asia/Colombo time zone and got the desired:

Apr 18, 2012 01:25 PM

Question: Doesn’t java.time require Android API level 26?

java.time works nicely on both 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 non-Android 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

Here is my solution that can be helpful for you

 public static void setFormattedEventDate(TextView textView,String start_date , String end_date){
        SimpleDateFormat formatIn = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat formatOut = new SimpleDateFormat("dd MMM");
        try {
            Date startDate = formatIn.parse(start_date);
            Date endDate = formatIn.parse(end_date);
            String outputStartDate = formatOut.format(startDate);
            String outputEndDate = formatOut.format(endDate);
            textView.setText(outputStartDate+" - "+outputEndDate);
        } catch (ParseException e) {
            e.printStackTrace();
        }

    }

Then call this method like this

setFormattedEventDate(contentViewHolder.rowItemEventBinding.txtCompetitionDate, event.datestart, event.dateend);
Md. Shofiulla
  • 2,135
  • 1
  • 13
  • 19
0

Try this:

  String dt =""; // your time value in there
  SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

                    Calendar c = Calendar.getInstance();
                    try {
                        c.setTime(sdf.parse(dt));
                    } catch (ParseException e) {
                        e.printStackTrace();
                    }
                    SimpleDateFormat sdf1 = new SimpleDateFormat("dd/MM/yyyy");//any format you want
                    String output = sdf1.format(c.getTime());