Problem converting date format in Java

后端 未结 6 1798
余生分开走
余生分开走 2021-01-21 11:58

I have a string in the form MMM/dd/yyyy, ie. May/21/2010. Now I want to convert it to yyyyMMdd, ie. 20100521.

My code is:

public static void main(String[         


        
6条回答
  •  滥情空心
    2021-01-21 12:38

    SimpleDateFormat is locale-dependent, and it's using your own locale by default. If you would like to use an English-based locale, you can create it by passing in a Locale when you create your SimpleDateFormat.

    So to use a US-based locale, change your SimpleDateFormat initialization to:

        DateFormat formatter = new SimpleDateFormat("MMM", Locale.US);
        DateFormat formatter2 = new SimpleDateFormat("MM", Locale.US);
    

提交回复
热议问题