Java 将日期转换为SimpleDataFormat中的字符串
我想将日期格式转换为字符串,但字符串输出不出来Java 将日期转换为SimpleDataFormat中的字符串,java,date,simpledateformat,Java,Date,Simpledateformat,我想将日期格式转换为字符串,但字符串输出不出来 DateFormat currentDate = new SimpleDateFormat("dd MM yyyy "); String currDateString = String.valueOf(currentDate); System.out.println(currDateString); 要格式化当前日期,您需要创建一个表示当前日期的date实例,然后使用SimpleDateFormat将其格式化为字符串 DateForm
DateFormat currentDate = new SimpleDateFormat("dd MM yyyy ");
String currDateString = String.valueOf(currentDate);
System.out.println(currDateString);
要格式化当前日期,您需要创建一个表示当前日期的
date
实例,然后使用SimpleDateFormat
将其格式化为字符串
DateFormat dateFormat = new SimpleDateFormat("dd MM yyyy");
String currDateString = dateFormat.format(new Date());
System.out.println(currDateString);
DateFormat是无状态的(与日历不同)@MauricePerry:无状态?我不知道;我不这么认为,SimpleDataFormat中有很多状态(这就是为什么它不是线程安全的)@rkosegi SimpleDataFormat是DateFormat的一个糟糕的实现。@MauricePerry:是的,那么为什么你说抽象类(DateFormat)是无状态的,而不知道实际实现的行为呢?这就像说某些接口是无状态的…@rkosegi好吧,让我们说它应该是无状态的。仅供参考,麻烦的旧日期时间类,如,和
java.text.SimpleTextFormat
,现在被类取代了。2017年01月24日我需要将其转换为2017年1月24日?新的SimpleDataFormat(“dd-MM-yyyy”)
是输入格式新SimpleDataFomat('dd-MMMM-yyyy')
是输出格式。你应该参考