简体   繁体   English

java-字符串到日期格式

[英]java - String to Date Format

I have a problem with String conversion to Date Format. 我有将String转换为Date格式的问题。 Please help me. 请帮我。 Below is my code: 下面是我的代码:

String strDate = "23/05/2012"; // Here the format of date is MM/dd/yyyy

Now i want to convert the above String to Date Format like " 23 May, 2012 ". 现在,我想将上述字符串转换为日期格式,例如“ 2012年5月23日 ”。

I am using below code but i am getting value as " Wed May 23 00:00:00 BOT 2012 " 我正在使用以下代码,但获得的价值为“ 2012年5月23日星期三00:00:00 BOT

String string = "23/05/2012";
Date date = new SimpleDateFormat("MM/dd/yyyy", Locale.ENGLISH).parse(string);
System.out.println(date); // Wed May 23 00:00:00 BOT 2012

How can i get the value as " 23 May, 2012 ". 我如何获得“ 2012年5月23日 ”的值。 Please help me friends.... 请帮我朋友...

You must render the date again. 您必须再次渲染日期。

You have the string and you parse it correctly back to a Date object. 您有了字符串,并将其正确解析回Date对象。 Now, you have to render that Date object the way you want. 现在,您必须以所需的方式渲染该Date对象。

You can use SimpleDateFormat again, changing the pattern. 您可以再次使用SimpleDateFormat更改模式。 Your code should then look like 您的代码应如下所示

String string = "23/05/2012";
Date date = new SimpleDateFormat("MM/dd/yyyy", Locale.ENGLISH).parse(string);
String newFormat = new SimpleDateFormat("dd MMMM, yyyy").format(date);
System.out.println(newFormat); // 23 May, 2012

Use the method format() from the class SimpleDateFormat with the correct pattern. 使用具有正确模式的类SimpleDateFormat format()方法。

Simple use: 使用简单:

SimpleDateFormat df = new SimpleDateFormat("dd MMM, yyyy");
System.out.println(df.format(date));
import java.text.*;
import java.util.*;


public class Main {
    public static void main(String[] args) throws ParseException{
        String strDate = "23/02/2012";
        Date date = new SimpleDateFormat("MM/dd/yyyy", Locale.ENGLISH).parse(strDate);
        String date1 = new SimpleDateFormat("dd MMMM, yyyy").format(date);
        System.out.println(date1);
    }
}

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

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