gpt4 book ai didi

java - 想要 "dd/MM/yyyy HH:mm:ss.SS"格式的当前日期和时间

转载 作者:太空狗 更新时间:2023-10-29 22:36:33 27 4
gpt4 key购买 nike

我正在使用以下代码以“dd/MM/yyyy HH:mm:ss.SS”格式获取日期。

    import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateAndTime{

public static void main(String[] args)throws Exception{

Calendar cal = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss.SS");
String strDate = sdf.format(cal.getTime());
System.out.println("Current date in String Format: "+strDate);

SimpleDateFormat sdf1 = new SimpleDateFormat();
sdf1.applyPattern("dd/MM/yyyy HH:mm:ss.SS");
Date date = sdf1.parse(strDate);
System.out.println("Current date in Date Format: "+date);

}
}

并得到以下输出

    Current date in String Format: 05/01/2012 21:10:17.287
Current date in Date Format: Thu Jan 05 21:10:17 IST 2012

请建议我应该如何以相同的字符串格式(dd/MM/yyyy HH:mm:ss.SS)显示日期,即我想要以下输出:

    Current date in String Format: 05/01/2012 21:10:17.287
Current date in Date Format: 05/01/2012 21:10:17.287

请建议

最佳答案

简单日期格式

sdf=new SimpleDateFormat("dd/MM/YYYY hh:mm:ss");
String dateString=sdf.format(date);

它将按照您的预期给出输出 28/09/2013 09:57:19

For complete program click here

关于java - 想要 "dd/MM/yyyy HH:mm:ss.SS"格式的当前日期和时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8745297/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com