2

I have problem formating date.

From : EEE, d MMM yyyy HH:mm:ss Z (example : Mon, 05 Jan 2014 15:10:00 +0200)

To : dd/MMM/yyyy HH:mm (example : 05/01/2014 15:10)

Here is what i tried :

private String formatDate(String date) {
    SimpleDateFormat format = new SimpleDateFormat("dd/MMM/yyyy HH:mm");
    Date dateResult = null;
    try {
        dateResult = format.parse(date);
    }
    catch (java.text.ParseException e) {
        Log.e(TAG, "", e);
    }
    return dateResult.toString();
}

I get exception : unparseable date at offset 0

some help would be nice here thanks ;)

An-droid
  • 6,433
  • 9
  • 48
  • 93
  • 1
    See [this](http://developer.android.com/reference/java/text/SimpleDateFormat.html) you can get some idea about all date formats. – Yugesh Feb 07 '14 at 09:46
  • I already got to see this, official doc is important so.. but it does not helps. My issue is not doc related but more about logic. But thanks – An-droid Feb 07 '14 at 09:50
  • @Yume117 You are parsing your *input* using the format you said you want for your *output*. Did you make a mistake in providing your sample code here, or is that cause of your problem? – Basil Bourque Feb 07 '14 at 10:28
  • Yes i've understood that far, i'm trying the posts below without good result for now – An-droid Feb 07 '14 at 10:29

4 Answers4

5

You need two times converting. For example:

private String formatDate(String date) {
    SimpleDateFormat formatFrom = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss Z ");

    java.util.Date tmpDate = formatFrom.parse(date);
    SimpleDateFormat formatTo = new SimpleDateFormat("dd/MMM/yyyy HH:mm");
    return formatTo.format(tmpDate);
}

For my own RSS parser I use the following code to parse different date formats:

    if (value.contains("+")) {
        value = value.substring(0, value.lastIndexOf("+") - 1);
    }

    String[] patterns = {//"EEE, dd MMM yyyy hh:mm:ss UTC",
            "yyyy.MM.dd G 'at' HH:mm:ss z",
            "EEE, MMM d, ''yy",
            "yyyyy.MMMMM.dd GGG hh:mm aaa",
            "EEE, d MMM yyyy HH:mm:ss Z",
            "yyMMddHHmmssZ",
            "d MMM yyyy HH:mm:ss z",
            "yyyy-MM-dd'T'HH:mm:ss",
            "yyyy-MM-dd'T'HH:mm:ss'Z'",
            "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'",
            "yyyy-MM-dd'T'HH:mm:ssZ",
            "yyyy-MM-dd'T'HH:mm:ss.SSSZ",
            "yyyy-MM-dd'T'HH:mm:ssz",
            "yyyy-MM-dd'T'HH:mm:ss.SSSz",
            "EEE, d MMM yy HH:mm:ssz",
            "EEE, d MMM yy HH:mm:ss",
            "EEE, d MMM yy HH:mm z",
            "EEE, d MMM yy HH:mm Z",
            "EEE, d MMM yyyy HH:mm:ss z",
            "EEE, d MMM yyyy HH:mm:ss Z",
            "EEE, d MMM yyyy HH:mm:ss ZZZZ",
            "EEE, d MMM yyyy HH:mm z",
            "EEE, d MMM yyyy HH:mm Z",
            "d MMM yy HH:mm z",
            "d MMM yy HH:mm:ss z",
            "d MMM yyyy HH:mm z",
            "d MMM yyyy HH:mm:ss z"};

    for (int i = 0; i < patterns.length; i++) {
        SimpleDateFormat sdf = new SimpleDateFormat(patterns[i], Locale.ENGLISH);
        try {
            pubdate = sdf.parse(value);

            break;
        } catch (Exception e) {
        }
    }
Roman Black
  • 3,501
  • 1
  • 22
  • 31
1

Try this

SimpleDateFormat form = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss Z");
SimpleDateFormat postFormater = new SimpleDateFormat("dd/MMM/yyyy HH:mm");

String inputdate = "Mon, 05 Jan 2014 15:10:00 +0200";

Date date = null;

    try {
        date = form.parse(inputdate);

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

    }

String resultdate = postFormater.format(date);
Chirag Ghori
  • 4,231
  • 2
  • 20
  • 35
1

Try code below:

String parseStringDate(String sqlDate) {
    String strDate = "";
    java.util.Date utilDate;

    SimpleDateFormat sqlDateFormat = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss Z");
    try {
        Calendar calTempDate = Calendar.getInstance();
        utilDate = sqlDateFormat.parse(sqlDate);

        calTempDate.setTime(utilDate);
        strDate = new SimpleDateFormat("dd/MMM/yyyy HH:mm").format(calTempDate.getTime());
    } catch (Exception e) {
        e.printStackTrace();
    }
    return strDate;
}
The Heist
  • 1,444
  • 1
  • 16
  • 32
0

Incorrect sample data

Your example data of Mon, 05 Jan 2014 15:10:00 +0200 is incorrect. That date was a Sunday, not a Monday.

java.time

The modern way is with the java.time classes.

Your input has a format that conforms with RFC 1123. The java.time classes include a formatter for that specific format.

String input = "Sun, 05 Jan 2014 15:10:00 +0200";
DateTimeFormatter f = DateTimeFormatter.RFC_1123_DATE_TIME ;
OffsetDateTime odt = OffsetDateTime.parse( input , f );

odt.toString(): 2014-01-05T15:10+02:00

See live code in IdeOne.com.

As for generating a string in other formats, that is already covered many times in Stack Overflow. Search for DateTimeFormatter class.


About java.time

The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.

The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.

To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.

With a JDBC driver complying with JDBC 4.2 or later, you may exchange java.time objects directly with your database. No need for strings or java.sql.* classes.

Where to obtain the java.time classes?

Community
  • 1
  • 1
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
  • Unable to parse Sunday, 18th February with EEEE, dd'th' MMMM not working ? – Sagar Oct 24 '18 at 13:09
  • @SagarHudge Post your own Question with specifics and with a [MCVE](https://stackoverflow.com/help/mcve). – Basil Bourque Oct 24 '18 at 15:41
  • @SagarHudge (a) Specify a `Locale`. (b) You **need a year** with that input. You cannot determine a day-of-week without a year. Otherwise, for a month and day without any year and without any time-of-day or time zone, use `YearMonth` class. `LocalDate.parse( "Sunday, 18th February 2018" , DateTimeFormatter.ofPattern( "EEEE, dd'th' MMMM uuuu" , Locale.US ) )` – Basil Bourque Oct 24 '18 at 18:14