format date from “MMM dd, yyyy HH:mm:ss a” to "MM.dd

后端 未结 2 545
野性不改
野性不改 2021-01-18 06:41

I want to format date from \"MMM dd, yyyy HH:mm:ss a\" to \"MM.dd\". I have following code

SimpleDateFormat ft = new SimpleDateFormat (\"MMM dd, yyyy hh:mm:s         


        
相关标签:
2条回答
  • 2021-01-18 07:27
    SimpleDateFormat sdf = new SimpleDateFormat("MM.dd", Locale.US);
    System.out.println("Formatted Date: " + sdf.format(date));
    
    0 讨论(0)
  • 2021-01-18 07:29

    Three possible explanations:

    1. your default locale is incompatible with the input date - e.g. it can't understand Sep as a month name
    2. there's something wrong with the input string, or
    3. t is the wrong type (e.g. java.sql.Date instead of java.util.Date, or some other type altogether), or is not declared.

    You should include details of the exception in your question to figure out which it is, but here's a working example using basically your own code, with the addition of a specific Locale.

    SimpleDateFormat ft = new SimpleDateFormat("MMM dd, yyyy hh:mm:ss a", Locale.US);
    java.util.Date t=ft.parse("Sep 16, 2015 10:34:23 AM");
    ft.applyPattern("MM.dd");
    System.out.println(ft.format(t));
    

    output:

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