Parse some string date to another format

为君一笑 提交于 2019-12-09 23:18:07

问题


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


回答1:


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



回答2:


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



回答3:


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



回答4:


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?

  • Java SE 8, Java SE 9, and later
    • Built-in.
    • Part of the standard Java API with a bundled implementation.
    • Java 9 adds some minor features and fixes.
  • Java SE 6 and Java SE 7
    • Much of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport.
  • Android
    • Later versions of Android bundle implementations of the java.time classes.
    • For earlier Android, the ThreeTenABP project adapts ThreeTen-Backport (mentioned above). See How to use ThreeTenABP….


来源:https://stackoverflow.com/questions/21624110/parse-some-string-date-to-another-format

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!