java.util.date

does System.currentTimeMillis() return UTC time?

99封情书 提交于 2019-11-27 05:11:14
问题 I want to get the current UTC time in millis. I searched google and got some answers that System.currentTimeMillis() does returns UTC time. but it does not. If I do following: long t1 = System.currentTimeMillis(); long t2 = new Date().getTime(); long t3 = Calendar.getInstance().getTimeInMillis(); all three times are almost same ( difference is in milli seconds due to calls ). t1 = 1372060916 t2 = 1372060917 t3 = 1372060918 and this time is not the UTC time instead this is my timezone time.

Do we have a TimeSpan sort of class in Java

北战南征 提交于 2019-11-27 04:44:07
I was just wondering if there is a need of TimeSpan in java.util so that I can define how much hours,minutes and seconds are there in between these two times. From this TimeSpan we can have a time interval between two times. like TimeSpan getTimeSpan( Date before, Date after ){...} or long timeSpan = System.currentTimeMillis(); // ... long job timeSpan = System.currentTimeMillis() - timeSpan; TimeSpan ts = new TimeSpan(timeSpan); and with this TimeSpan we can use it in SimpleDateFormat . SimpleDateFormat format = new SimpleDateFormat("HH:mm:ss"); format.format( timsSpan ); I am not sure if

return date type with format in java [duplicate]

前提是你 提交于 2019-11-27 02:19:30
This question already has an answer here: Change the format of Date Java [closed] 2 answers I'm trying implement get method for variable has type "Date". But when return, I want to return it with format "yyyy/MM/dd" and MUST be Date type. Example: original date: 2018/01/01T15:00.00.000+0000 I want return: 2018-01-01 and MUST be date, not string Can you help me in this case ? A Date is basically stored as a number of milliseconds since the epoch (1/1/1970). It can be formatted into a String in various ways (e.g. with SimpleDateFormat ), but it is never stored as a String. So, what you're asking

How to convert Joda-Time DateTime to java.util.Date and vice versa?

自作多情 提交于 2019-11-27 00:15:57
问题 Is it possible to do that? If yes, then how do I do the conversion from Joda-Time to Date and vice versa? 回答1: To convert Java Date to Joda DateTime:- Date date = new Date(); DateTime dateTime = new DateTime(date); And vice versa:- Date dateNew = dateTime.toDate(); With TimeZone , if required:- DateTime dateTimeNew = new DateTime(date.getTime(), timeZone); Date dateTimeZone = dateTime.toDateTimeAtStartOfDay(timeZone).toDate(); 回答2: You haven't specified which type within Joda Time you're

How to convert ZonedDateTime to Date?

三世轮回 提交于 2019-11-27 00:05:10
问题 I am trying to set a server agnostic date time in my database and I believe the best practice to do so is to set a UTC DateTime. My db server is Cassandra and the db driver for Java understands only the Date type. So assuming that in my code I am using the new Java 8 ZonedDateTime to get the UTC now ( ZonedDateTime.now(ZoneOffset.UTC) ), how can I convert this ZonedDateTime instance to the "legacy" Date class? 回答1: You can convert ZonedDateTime to an instant, which you can use directly with

java.util.Date is generating a wrong date?

荒凉一梦 提交于 2019-11-26 23:43:06
问题 Here's my code: java.util.Date TODAY = new java.util.Date(); SimpleDateFormat SDF = new SimpleDateFormat( "YYYY-MM-DD" ); System.out.println ( SDF.format( TODAY ) );' And the result is: 2015-02-33 But today's date is 2015-02-02! What may be the reason behind this wrong output? 回答1: What may be the reason behind this Wrong Output ? Your assumptions about the date format string are wrong, the output is correct . y Year Y Week year D Day in year d Day in month M Month in year m Minute in hour

return date type with format in java [duplicate]

青春壹個敷衍的年華 提交于 2019-11-26 22:15:15
问题 This question already has answers here : Change the format of Date Java [closed] (2 answers) Closed last year . I'm trying implement get method for variable has type "Date". But when return, I want to return it with format "yyyy/MM/dd" and MUST be Date type. Example: original date: 2018/01/01T15:00.00.000+0000 I want return: 2018-01-01 and MUST be date, not string Can you help me in this case ? 回答1: A Date is basically stored as a number of milliseconds since the epoch (1/1/1970). It can be

How to subtract X day from a Date object in Java?

为君一笑 提交于 2019-11-26 21:52:23
I want to do something like: Date date = new Date(); // current date date = date - 300; // substract 300 days from current date and I want to use this "date" How to do it? Jigar Joshi Java 8 and later With Java 8's date time API change, Use LocalDate LocalDate date = LocalDate.now().minusDays(300); Java 7 and earlier Use Calendar 's add() method Calendar cal = Calendar.getInstance(); cal.setTime(dateInstance); cal.add(Calendar.DATE, -30); Date dateBefore30Days = cal.getTime(); @JigarJoshi it's the good answer, and of course also @Tim recommendation to use . joda-time . I only want to add more

Calculating the difference between two Java date instances

谁说胖子不能爱 提交于 2019-11-26 17:58:51
I'm using Java's java.util.Date class in Scala and want to compare a Date object and the current time. I know I can calculate the delta by using getTime(): (new java.util.Date()).getTime() - oldDate.getTime() However, this just leaves me with a long representing milliseconds. Is there any simpler, nicer way to get a time delta? notnoop The JDK Date API is horribly broken unfortunately. I recommend using Joda Time library . Joda Time has a concept of time Interval : Interval interval = new Interval(oldTime, new Instant()); EDIT: By the way, Joda has two concepts: Interval for representing an

How to add minutes to my Date

允我心安 提交于 2019-11-26 12:38:03
问题 I have this date object: SimpleDateFormat df = new SimpleDateFormat(\"yyyy-mm-dd HH:mm\"); Date d1 = df.parse(interviewList.get(37).getTime()); value of d1 is Fri Jan 07 17:40:00 PKT 2011 Now I am trying to add 10 minutes to the date above. Calendar cal = Calendar.getInstance(); cal.setTime(d1); cal.add(Calendar.MINUTE, 10); String newTime = df.format(cal.getTime()); Value of newTime changes to 2011-50-07 17:50 but it should be 07-01-2011 17:50 . It adds minutes correctly but it also changes