简体   繁体   English

将一个日期字符串转换为另一个Java

[英]transforming one date string into another java

I looked around here and could not find anything that matches what I need. 我环顾四周,找不到符合我需求的任何东西。

I get the following String: 我得到以下字符串:

"March 13, 2013"

and need to transform it into 并且需要将其转化为

"2013-03-13"

I tried using this code: 我尝试使用此代码:

    SimpleDateFormat inputFormat = new SimpleDateFormat("MMMMMMMMMM dd, YYYY");
    SimpleDateFormat outputFormat = new SimpleDateFormat("YYYY-MM-dd");
    Date d = inputFormat.parse(startDate);
    System.out.println(outputFormat.format(d));

but i get "203-12-30".. Why is that? 但是我得到“ 203-12-30”。为什么?

Y represents week year. Y代表星期年。 Try using y 尝试使用y

SimpleDateFormat outputFormat = new SimpleDateFormat("yyyy-MM-dd");

I just copied and pasted your code made a few small changes and got the result you were looking for. 我只是复制并粘贴了您的代码,做了一些小的更改,并得到了您想要的结果。 The main change I made was changing the upper case "Y" in inputFormat and outputFormat to lower case "y". 我所做的主要更改是将inputFormat和outputFormat中的大写“ Y”更改为小写“ y”。 Anyway here you are: 无论如何,您是:

String startDate = "March 13, 2013";
SimpleDateFormat inputFormat = new SimpleDateFormat("MMMMMMMMMM dd, yyyy");
SimpleDateFormat outputFormat = new SimpleDateFormat("yyyy-MM-dd");

try {
    Date d = inputFormat.parse(startDate);
    System.out.println(outputFormat.format(d));
} catch (ParseException e) {
    e.printStackTrace();
}

Have a try with the following code: 尝试以下代码:

String startDate = "March 13, 2013";
    SimpleDateFormat inputFormat = new SimpleDateFormat("MMMM dd, yyyy",Locale.ENGLISH);
    SimpleDateFormat outputFormat = new SimpleDateFormat("yyyy-MM-dd");
    Date d = inputFormat.parse(startDate);
    System.out.println(outputFormat.format(d));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM