string-to-date格式

kknvjkwl  于 2021-06-30  发布在  Java
关注(0)|答案(3)|浏览(462)

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

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

现在我想把上面的字符串转换成日期格式,比如“2012年5月23日”。
我正在使用下面的代码,但我得到的值为“wed may 23 00:00:00 bot 2012”

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日”。请帮助我的朋友。。。。

t9eec4r0

t9eec4r01#

必须重新呈现日期。
你有了这个字符串,并将它正确地解析回 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
apeeds0o

apeeds0o2#

使用方法 format() 来自班级 SimpleDateFormat 以正确的模式。
使用简单:

SimpleDateFormat df = new SimpleDateFormat("dd MMM, yyyy");
System.out.println(df.format(date));
zyfwsgd6

zyfwsgd63#

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);
    }
}

相关问题