繁体   English   中英

java-字符串到日期格式

[英]java - String to Date Format

我有将String转换为Date格式的问题。 请帮我。 下面是我的代码:

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

现在,我想将上述字符串转换为日期格式,例如“ 2012年5月23日 ”。

我正在使用以下代码,但获得的价值为“ 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

我如何获得“ 2012年5月23日 ”的值。 请帮我朋友...

您必须再次渲染日期。

您有了字符串,并将其正确解析回Date对象。 现在,您必须以所需的方式渲染该Date对象。

您可以再次使用SimpleDateFormat更改模式。 您的代码应如下所示

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

使用具有正确模式的类SimpleDateFormat format()方法。

使用简单:

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