I have data from API, the data is date with format "2018-07-09". How to change the format to Monday, July 9 , 2018 in android studio?
            Asked
            
        
        
            Active
            
        
            Viewed 1,329 times
        
    -1
            
            
        - 
                    *"..in android studio?"* The same way you'd do it in any IDE that handles Android code. Write the code for it. (I.E. the IDE is irrelevant.) – Andrew Thompson Jul 09 '18 at 16:14
- 
                    Please search before asking. In this particular case it shouldn’t be too hard to dig up the first 100 similar questions. – Ole V.V. Jul 09 '18 at 19:27
- 
                    Similar, for example: [java.util.Date format conversion yyyy-mm-dd to mm-dd-yyyy](https://stackoverflow.com/questions/18480633/java-util-date-format-conversion-yyyy-mm-dd-to-mm-dd-yyyy). – Ole V.V. Jul 10 '18 at 09:30
- 
                    Possible duplicate of [Converting Date format to other format on Java](https://stackoverflow.com/questions/18396543/converting-date-format-to-other-format-on-java) – Jon Jul 11 '18 at 03:07
3 Answers
1
            You can parse string to object them format it with DateTimeFormatter:
DateTimeFormatter parser = DateTimeFormatter.ofPattern("yyyy-MM-dd");
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("EEEE, MMMM d, yyyy", Locale.ENGLISH);
System.out.println(formatter.format(parser.parse( "2018-07-09"))); // Monday, July 9, 2018
 
    
    
        xingbin
        
- 27,410
- 9
- 53
- 103
0
            
            
        SimpleDateFormat fromApi = new SimpleDateFormat("yyyy-MM-dd");
SimpleDateFormat myFormat = new SimpleDateFormat("EEEE, MMMM d, yyyy");
try {
    String reformattedStr = myFormat.format(fromApi.parse(inputString));
} catch (ParseException e) {
    e.printStackTrace();
}
See oracle doc for more understanding.
 
    
    
        Khemraj Sharma
        
- 57,232
- 27
- 203
- 212
0
            
            
            DateTimeFormatter dateFormatter = DateTimeFormatter
            .ofLocalizedDate(FormatStyle.FULL)
            .withLocale(Locale.US);
    LocalDate date = LocalDate.parse("2018-07-09");
    String formattedDate = date.format(dateFormatter);
    System.out.println(formattedDate);
This prints:
Monday, July 9, 2018
Messages:
- The date string you get from the API, 2018-07-09, is in ISO 8601 format.LocalDatefromjava.time, the modern Java date and time API, parses this format as its default, that is, without any explicit formatter. So don’t go to the trouble of creating one.
- For display to the user use the built-in formats. You get them from the DateTimeFormatter.ofLocalizedXxxxmethods and may adapt them to the user’s locale as shown in the above code.
- You tagged your question simpledateformat. The SimpleDateFormatclass is long outdated and notoriously troublesome, so please avoid it.java.timeis much nicer to work with.
Question: Can I use java.time on Android?
Yes, java.time works nicely on 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, I’m told) the modern API comes built-in.
- In Java 6 and 7 get the ThreeTen Backport, the backport of the new 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.bpwith subpackages.
Links
- Oracle tutorial: Date Time explaining how to use java.time.
- Java Specification Request (JSR) 310, where java.timewas first described.
- ThreeTen Backport project, the backport of java.timeto 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.
- Wikipedia article: ISO 8601
 
    
    
        Ole V.V.
        
- 81,772
- 15
- 137
- 161
