在Java中创建日期的正确方法是什么? [英] What's the right way to create a date in Java?
问题描述
我对Date类的Java API感到困惑。一切似乎已被弃用,并链接到Calendar类。所以我开始使用日历对象来做我想用一个日期,但直观地,当我真正想做的是创建和比较两个日期时,我很想使用一个日历对象。
I get confused by the Java API for the Date class. Everything seems to be deprecated and links to the Calendar class. So I started using the Calendar objects to do what I would have liked to do with a Date, but intuitively it kind of bothers me to use a Calendar object when all I really want to do is create and compare two dates.
有一个简单的方法吗?现在我做
Is there a simple way to do that? For now I do
Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(0);
cal.set(year, month, day, hour, minute, second);
Date date = cal.getTime(); // get back a Date object
推荐答案
a href =http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html> SimpleDateFormat
You can use SimpleDateFormat
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
Date d = sdf.parse("21/12/2012");
但我不知道是否应该更多考虑 right 使用日历...
But I don't know whether it should be considered more right than to use Calendar ...
这篇关于在Java中创建日期的正确方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!