Questions tagged [java.util.date]

Through Java 7, the official class for representing a moment in time, using a thin wrapper around the number of milliseconds from epoch (1970-01-01). In Java 8, java.time.Instant and related classes were introduced as a replacement.

In the JDK through Java 7, the java.util.Date class is very widely used and contrary to what its name suggests, represents an instant rather than a "date" (i.e. a particular day). Date objects are mutable.

In Java 8, a new package java.time was introduced, which contains java.time.Instant to be used to represent an instant. This package should be preferred over using the old java.util.Date classes when writing new code.

323 questions
498
votes
46 answers

Calculating the difference between two Java date instances

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…
pr1001
  • 21,727
  • 17
  • 79
  • 125
456
votes
17 answers

Convert java.util.Date to String

I want to convert a java.util.Date object to a String in Java. The format is 2010-05-30 22:15:52
novicePrgrmr
  • 18,647
  • 31
  • 81
  • 103
287
votes
12 answers

How to set time zone of a java.util.Date?

I have parsed a java.util.Date from a String but it is setting the local time zone as the time zone of the date object. The time zone is not specified in the String from which Date is parsed. I want to set a specific time zone of the date…
Amit
  • 33,847
  • 91
  • 226
  • 299
218
votes
10 answers

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

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?
yetAnotherSE
  • 3,178
  • 6
  • 26
  • 33
150
votes
9 answers

How to convert ZonedDateTime to Date?

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…
120
votes
13 answers

Why is the month changed to 50 after I added 10 minutes?

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…
junaidp
  • 10,801
  • 29
  • 89
  • 137
115
votes
3 answers

does System.currentTimeMillis() return UTC time?

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…
g.revolution
  • 11,962
  • 23
  • 81
  • 107
78
votes
3 answers

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

Is it possible to do that? If yes, then how do I do the conversion from Joda-Time to Date and vice versa?
Time
  • 1,551
  • 2
  • 13
  • 14
73
votes
10 answers

Get the current date in java.sql.Date format

I need to add the current date into a prepared statement of a JDBC call. I need to add the date in a format like yyyy/MM/dd. I've try with DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd"); Date date = new Date(); pstm.setDate(6,…
giozh
  • 9,868
  • 30
  • 102
  • 183
59
votes
4 answers

Do we have a TimeSpan sort of class in Java

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…
Talha Ahmed Khan
  • 15,043
  • 10
  • 42
  • 49
33
votes
11 answers

java.util.Date: seven days ago

I have a report created in Jasper Reports which ONLY recognizes java.util.Date's (not Calendar or Gregorian, etc). Is there a way to create a date 7 days prior to the current date? Ideally, it would look something like this: new Date(New Date() -…
Ramy
  • 20,541
  • 41
  • 103
  • 153
25
votes
2 answers

Why shouldn't I use date4j instead of joda java.util.Calendar or jsr 310?

I recently came across date4j, an extremely simple library (essentially a single class) for working with dates in Java. Conceptually, I really like the "idea" of date4j. In fact, after reading both the entire main site and the documentation in the…
nogridbag
  • 3,521
  • 4
  • 38
  • 51
22
votes
8 answers

How to remove milliseconds from Date Object format in Java

Since the java.util.Date object stores Date as 2014-01-24 17:33:47.214, but I want the Date format as 2014-01-24 17:33:47. I want to remove the milliseconds part. I checked a question related to my question... How to remove sub seconds part of Date…
earthmover
  • 4,395
  • 10
  • 43
  • 74
19
votes
1 answer

Are java.util.Date and java.util.Calendar deprecated?

It seems that the new java.time API offers everything from java.util.Date and much more. Is there any reason to use java.util.Date when the newer java.time API is there since Java 8? Should java.util.Date and java.util.Calendar be avoided…
Ondrej Bozek
  • 10,987
  • 7
  • 54
  • 70
18
votes
1 answer

How to convert java.util.Date to Java8 java.time.YearMonth

How can I best convert a java.util.Date to a Java 8 java.time.YearMonth? Unfortunately the following throws a DateTimeException: YearMonth yearMonth = YearMonth.from(date.toInstant()); results in: java.time.DateTimeException: Unable to obtain…
Sebastian
  • 877
  • 1
  • 9
  • 20
1
2 3
21 22