如何在java中将当前日期转换为字符串?
如何在Java中将当前日期转换为字符串?
9个解决方案
198 votes
String date = new SimpleDateFormat("dd-MM-yyyy").format(new Date());
Ian Purton answered 2019-07-22T17:00:10Z
59 votes
// GET DATE & TIME IN ANY FORMAT
import java.util.Calendar;
import java.text.SimpleDateFormat;
public static final String DATE_FORMAT_NOW = "yyyy-MM-dd HH:mm:ss";
public static String now() {
Calendar cal = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_NOW);
return sdf.format(cal.getTime());
}
取自这里
Itay Karo answered 2019-07-22T17:00:29Z
25 votes
// On the form: dow mon dd hh:mm:ss zzz yyyy
new Date().toString();
jasonmp85 answered 2019-07-22T17:00:47Z
17 votes
使用SimpleDateFormat实施; 例如SimpleDateFormat。
DateFormat df = new SimpleDateFormat("dd/MM/yyyy");
String data = df.format(new Date());
Adamski answered 2019-07-22T17:01:13Z
9 votes
快点 :
String date = FastDateFormat.getInstance("dd-MM-yyyy").format(System.currentTimeMillis( ));
Johann Goulley answered 2019-07-22T17:01:40Z
7 votes
TL;博士
LocalDate.now()
.toString()
2017年1月23日
最好明确指定所需/预期的时区。
LocalDate.now( ZoneId.of( "America/Montreal" ) )
.toString()
java.time
Java 8及更高版本的现代方法是使用java.time框架。
指定时区,因为在任何给定时刻,日期在世界各地变化。
ZoneId zoneId = ZoneId.of( "America/Montreal" ) ; // Or ZoneOffset.UTC or ZoneId.systemDefault()
LocalDate today = LocalDate.now( zoneId ) ;
String output = today.toString() ;
2017年1月23日
默认情况下,您将获得标准ISO 8601格式的字符串。
对于其他格式,请使用YearQuarter类。
关于java.time
java.time框架内置于Java 8及更高版本中。 这些类取代了麻烦的旧遗留日期时间类,例如YearQuarter,YearQuarter,&YearQuarter。
现在处于维护模式的Joda-Time项目建议迁移到java.time类。
要了解更多信息,请参阅Oracle教程。 并搜索Stack Overflow以获取许多示例和解释。 规范是JSR 310。
您可以直接与数据库交换java.time对象。 使用符合JDBC 4.2或更高版本的JDBC驱动程序。 不需要字符串,不需要YearQuarter类。
从哪里获取java.time类?
Java SE 8,Java SE 9,Java SE 10及更高版本内置。
带有捆绑实现的标准Java API的一部分。
Java 9增加了一些小功能和修复。
Java SE 6和Java SE 7许多java.time功能都被反向移植到Java 6& 7在ThreeTen-Backport。
Android的更高版本的Android捆绑java.time类的实现。
对于早期的Android(< 26),ThreeTenABP项目采用ThreeTen-Backport(如上所述)。 请参见如何使用ThreeTenABP ....
ThreeTen-Extra项目使用其他类扩展了java.time。 该项目是未来可能添加到java.time的试验场。 您可以在这里找到一些有用的类,例如YearQuarter,YearQuarter,YearQuarter等。
Basil Bourque answered 2019-07-22T17:03:37Z
0 votes
大多数答案都是有效的。用于日期处理的新JAVA API修改确保减少了Java日期处理中的某些早期歧义。
您将收到类似呼叫的已弃用消息。
new Date() // deprecated
上面的调用让开发人员假设一个新的Date对象将为Date对象提供当前时间戳。 此行为在其他Java API类中不一致。
执行此操作的新方法是使用日历实例。
new SimpleDateFormat("yyyy-MM-dd").format(Calendar.getInstance().getTime()
命名惯例也不完美,但这种组织非常有条理。对于像我这样的人来说,如果听起来似乎是合乎逻辑的话,他们很难把事情搞砸但永远不会忘记,这是一个很好的方法。
这更像是现实生活的代名词
我们得到一个Calendar对象,然后在其中查找时间。 (你一定想知道没有人从日历中获取时间,这就是我说它不完美的原因。但这是一个不同的话题共)
然后我们想要一个简单的文本格式的日期,所以我们使用SimpleDateFormat实用程序类,它帮助我们格式化步骤1中的日期。我已经使用yyyy,MM,dd作为格式中的参数。 支持的日期格式参数
另一种方法是使用Joda time API
new DateTime().toString("yyyy-MM-dd")
或者很明显
new DateTime(Calendar.getInstance().getTime()).toString("yyyy-MM-dd")
两者都会返回相同的结果。
Swapnil Vargaonkar answered 2019-07-22T17:05:30Z
0 votes
时间为YYYY-MM-dd
String time = new DateTime( yourData ).toString("yyyy-MM-dd");
而DateTime库则是:
import org.joda.time.DateTime;
answered 2019-07-22T17:06:10Z
-3 votes
public static Date getDateByString(String dateTime) {
if(dateTime==null || dateTime.isEmpty()) {
return null;
}
else{
String modified = dateTime + ".000+0000";
DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
Date dateObj = new Date();
Date dateObj1 = new Date();
try {
if (dateTime != null) {
dateObj = formatter.parse(modified);
}
} catch (ParseException e) {
e.printStackTrace();
}
return dateObj;
}
}
Ganesh Reddy answered 2019-07-22T17:06:27Z