Change date string format in android

后端 未结 6 652
忘了有多久
忘了有多久 2020-12-01 04:14

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

相关标签:
6条回答
  • 2020-12-01 04:45

    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";
    
    0 讨论(0)
  • 2020-12-01 04:47

    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

    • Oracle tutorial: Date Time explaining how to use java.time.
    • Java Specification Request (JSR) 310, where java.time was first described.
    • ThreeTen Backport project, the backport of java.time to Java 6 and 7 (ThreeTen for JSR-310).
    • ThreeTenABP, Android edition of ThreeTen Backport
    • Question: How to use ThreeTenABP in Android Project, with a very thorough explanation.
    0 讨论(0)
  • 2020-12-01 04:49

    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);
    
    0 讨论(0)
  • 2020-12-01 04:57

    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());
    
    0 讨论(0)
  • 2020-12-01 04:59
    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);
    
    0 讨论(0)
  • 2020-12-01 05:03

    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

    0 讨论(0)
提交回复
热议问题