Lax*_*ana 62 java datetime date
我想从Date对象中删除时间.
DateFormat df;
String date;
df = new SimpleDateFormat("dd/MM/yyyy");
d = eventList.get(0).getStartDate(); // I'm getting the date using this method
date = df.format(d); // Converting date in "dd/MM/yyyy" format
Run Code Online (Sandbox Code Playgroud)
但是当我转换这个日期(String格式)时,它也会增加时间.
我根本不想要时间.我想要的只是"21/03/2012".
Sun*_*eri 110
您可以通过将hour,minute,second和millisecond值设置为零来从java.util.Date中删除时间部分.
import java.util.Calendar;
import java.util.Date;
public class DateUtil {
public static Date removeTime(Date date) {
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.set(Calendar.HOUR_OF_DAY, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);
return cal.getTime();
}
}
Run Code Online (Sandbox Code Playgroud)
Ran*_*Lin 43
快速回答是:
不,你不被允许这样做.因为那是Date用的.
来自javadoc Date:
Date类表示特定的时刻,精度为毫秒.
但是,因为这个类只是一个数据对象.它并不关心我们如何描述它.当我们看到日期时2012/01/01 12:05:10.321,我们可以说它是2012/01/01,这就是你需要的.有很多方法可以做到这一点.
示例1:通过操纵字符串
输入字符串:2012/01/20 12:05:10.321
期望的输出字符串:2012/01/20
由于yyyy/MM/dd正是我们所需要的,我们可以简单地操纵字符串来获得结果.
String input = "2012/01/20 12:05:10.321";
String output = input.substring(0, 10); // Output : 2012/01/20
Run Code Online (Sandbox Code Playgroud)
示例2:通过SimpleDateFormat
输入字符串:2012/01/20 12:05:10.321
所需的输出字符串:01/20/2012
在这种情况下,我们需要不同的格式.
String input = "2012/01/20 12:05:10.321";
DateFormat inputFormatter = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss.SSS");
Date date = inputFormatter.parse(input);
DateFormat outputFormatter = new SimpleDateFormat("MM/dd/yyyy");
String output = outputFormatter.format(date); // Output : 01/20/2012
Run Code Online (Sandbox Code Playgroud)
要使用SimpleDateFormat,请检查SimpleDateFormat JavaDoc.
jse*_*als 34
Apache Commons DateUtils有一个"truncate"方法,我刚才这样做,我认为它会满足你的需求.它真的很容易使用:
DateUtils.truncate(dateYouWantToTruncate, Calendar.DAY_OF_MONTH);
Run Code Online (Sandbox Code Playgroud)
DateUtils还有许多其他酷工具,如"isSameDay()"等.看看吧!它可能会让事情变得更容易.
小智 12
那这个呢:
Date today = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
today = sdf.parse(sdf.format(today));
Run Code Online (Sandbox Code Playgroud)
你想要的是不可能的.
一个Date对象代表时间"绝对"的时刻.你不能"删除它的时间部分".Date直接打印对象时System.out.println(date),它将始终以包含时间的默认格式进行格式化.你无能为力改变这一点.
Date而不是以某种方式尝试使用类不是为其设计的东西,你应该寻找另一种解决方案.例如,用于SimpleDateFormat以您想要的任何格式格式化日期.
遗憾的是,Java日期和日历API并不是标准Java API中设计最好的类.有一个名为Joda-Time的库,它有一个更好,更强大的API.
Joda-Time有许多特殊的课程来支持日期,时间,时间段,持续时间等.如果你想要一个没有时间的日期,那么Joda-Time的LocalDate课程就是你要用的.
Date dateWithoutTime =
new Date(myDate.getYear(),myDate.getMonth(),myDate.getDate())
Run Code Online (Sandbox Code Playgroud)
这已被弃用,但这是最快的方法。
小智 5
可能下面的代码可以帮助正在寻找零时的人:
Date todayDate = new Date();
GregorianCalendar todayDate_G = new GregorianCalendar();
gcd.setTime(currentDate);
int _Day = todayDate_GC.get(GregorianCalendar.DAY_OF_MONTH);
int _Month = todayDate_GC.get(GregorianCalendar.MONTH);
int _Year = todayDate_GC.get(GregorianCalendar.YEAR);
GregorianCalendar newDate = new GregorianCalendar(_Year,_Month,_Day,0,0,0);
zeroHourDate = newDate.getTime();
long zeroHourDateTime = newDate.getTimeInMillis();
Run Code Online (Sandbox Code Playgroud)
希望这会有所帮助。