Conversion of Date

后端 未结 4 570
醉话见心
醉话见心 2020-12-12 07:04

I am getting date in two different formats.
1) 2012-01-05
2) 05/01/2012
But I want this to be in the below format. \"5 Jan 2011\"

相关标签:
4条回答
  • 2020-12-12 07:35
    SimpleDateFormat formatter = new SimpleDateFormat("dd MMM yyyy");  
    String now = formatter.format(new Date());
    

    this u want right...

    or

    String oldString = "2009-12 Dec";
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(new SimpleDateFormat("yyyy-MM").parse(oldString)); // Yes, month name is ignored but we don't need this.
    calendar.set(Calendar.DATE, calendar.getActualMaximum(Calendar.DATE));
    String newString = new SimpleDateFormat("dd-MMM-yyyy").format(calendar.getTime()).toUpperCase();
    System.out.println(newString); // 31-DEC-2009
    
    0 讨论(0)
  • 2020-12-12 07:35

    This should retrieve the date in the format you want. Simple few lines.

                String pattern = "dd MMM yyyy";
                SimpleDateFormat format = new SimpleDateFormat(pattern);
    
                System.out.println(format.format(new Date()));
    
    0 讨论(0)
  • 2020-12-12 07:59

    Take a look here: How to parse a date?

    You have to determine, which format is used and than create a appropriate pattern.

    After parsing the date, you can format it with another pattern, if you need a String:

    SimpleDateFormat formatter = new SimpleDateFormat("dd MMM yyyy");  
    String now = formatter.format(new Date());
    
    0 讨论(0)
  • 2020-12-12 07:59
    String d1 = "2012-01-05";
    String d2 = "05/01/2012";
    SimpleDateFormat curFormater1 = new SimpleDateFormat("yyyy-MM-dd");
    SimpleDateFormat curFormater2 = new SimpleDateFormat("dd/MM/yyyy");
    Date dateObj1 = null;
    
    Date dateObj2 = null;
    try {
        dateObj1 = curFormater.parse(d1);
        dateObj2 = curFormater.parse(d2);
    } catch (ParseException e) {
        e.printStackTrace();
    }
    
    SimpleDateFormat formatter = new SimpleDateFormat("dd MMM yyyy");
    String date_new1 = formatter.format(d1);
    String date_new2 = formatter.format(d2);
    

    try this code it will help you so solve your problem

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