How to convert Date to a particular format in android?

后端 未结 7 1687
时光说笑
时光说笑 2020-11-27 07:06

\"Mar 10, 2016 6:30:00 PM\" This is my date and I want to convert this into \"10 Mar 2016\". Can I use SimpleDateFormat in android. I am not getting the exact pattern to con

相关标签:
7条回答
  • 2020-11-27 07:46

    You should parse() the String into Date and then format it into the desired format. You can use MMM dd, yyyy HH:mm:ss a format to parse the given String.

    Here is the code snippet:

    public static void main (String[] args) throws Exception
    {
        String date = "Mar 10, 2016 6:30:00 PM";
        SimpleDateFormat spf = new SimpleDateFormat("MMM dd, yyyy hh:mm:ss a");
        Date newDate = spf.parse(date);
        spf = new SimpleDateFormat("dd MMM yyyy");
        String newDateString = spf.format(newDate);
        System.out.println(newDateString);
    }
    

    Output:

    10 Mar 2016
    
    0 讨论(0)
  • 2020-11-27 07:46
    public static String formatDate(String fromFormat, String toFormat, String dateToFormat) {
        SimpleDateFormat inFormat = new SimpleDateFormat(fromFormat);
        Date date = null;
        try {
            date = inFormat.parse(dateToFormat);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        SimpleDateFormat outFormat = new SimpleDateFormat(toFormat);
    
        return outFormat.format(date);
    }
    

    Use: formatDate("dd-MM-yyyy", "EEEE, dd MMMM yyyy","26-07-2019");

    Result: Friday, 26 July 2019

    0 讨论(0)
  • 2020-11-27 07:49

    I have created a method for this problem. We simply need to pass Current date format, required date format and Date String.

    private String changeDateFormat(String currentFormat,String requiredFormat,String dateString){
        String result="";
        if (Strings.isNullOrEmpty(dateString)){
            return result;
        }
        SimpleDateFormat formatterOld = new SimpleDateFormat(currentFormat, Locale.getDefault());
        SimpleDateFormat formatterNew = new SimpleDateFormat(requiredFormat, Locale.getDefault());
        Date date=null;
        try {
            date = formatterOld.parse(dateString);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        if (date != null) {
            result = formatterNew.format(date);
        }
        return result;
    }
    

    This method will return Date String in format you require. In your case method call will be:

    String date = changeDateFormat("MMM dd, yyyy hh:mm:ss a","dd MMM yyyy","Mar 10, 2016 6:30:00 PM");
    
    0 讨论(0)
  • 2020-11-27 07:50

    For the sake of completeness, here is the modern version. This is for anyone reading this who either uses Java 8 or later or is happy with a (good and futureproof) external library.

        String date = "Mar 10, 2016 6:30:00 PM";
        DateTimeFormatter parseFormatter 
                = DateTimeFormatter.ofPattern("MMM d, uuuu h:mm:ss a", Locale.ENGLISH);
        DateTimeFormatter newFormatter 
                = DateTimeFormatter.ofPattern("d MMM uuuu", Locale.ENGLISH);
        date = LocalDateTime.parse(date, parseFormatter).format(newFormatter);
        System.out.println(date);
    

    This prints the desired

    10 Mar 2016
    

    Please note the use of explicit locale for both DateTimeFormatter objects. “Mar” and “PM” both are in English, so neither the parsing nor the formatting will work unless some English-speaking locale is used. By giving it explicitly we are making the code robust enough to behave as expected also on computers and JVMs with other default locales.

    To use the above on Android, use ThreeTenABP, please see How to use ThreeTenABP in Android Project. On other Java 6 and 7 use ThreeTen Backport.

    0 讨论(0)
  • 2020-11-27 07:53

    This is modified code that you should use:

    String date="Mar 10, 2016 6:30:00 PM";
    SimpleDateFormat spf=new SimpleDateFormat("MMM dd, yyyy hh:mm:ss aaa");
    Date newDate=spf.parse(date);
    spf= new SimpleDateFormat("dd MMM yyyy");
    date = spf.format(newDate);
    System.out.println(date);
    

    Use hh for hours in order to get correct time.

    Java 8 and later

    Java 8 introduced new classes for time manipulation, so use following code in such cases:

        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MMM dd, yyyy h:mm:ss a");
        LocalDateTime dateTime = LocalDateTime.parse(date, formatter);
        DateTimeFormatter formatter2 = DateTimeFormatter.ofPattern("dd MMM yyyy");
        System.out.println(dateTime.format(formatter2));
    

    Use h for hour format, since in this case hour has only one digit.

    0 讨论(0)
  • 2020-11-27 07:59

    conversion from string to date and date to string

    String deliveryDate="2018-09-04";                       
    SimpleDateFormat dateFormatprev = new SimpleDateFormat("yyyy-MM-dd");
    Date d = dateFormatprev.parse(deliveryDate);
    SimpleDateFormat dateFormat = new SimpleDateFormat("EEE dd MMM yyyy");
    String changedDate = dateFormat.format(d);
    
    0 讨论(0)
提交回复
热议问题