1

I have a Date . I want to copy its time only, excluding the date. I then need to insert the time into another date.

Is there a easy way to do this?. I cant change the Date datatype. I can do, getHour(),getMinutes() etc however this is long winded. is there a clearner version that i could use ? or perhaps other library's like apache commons to set/get date time (have not spotten anything so far).

5
  • Use Java 8: oracle.com/technetwork/articles/java/… Commented Nov 20, 2014 at 15:36
  • unable to use java 8 Commented Nov 20, 2014 at 15:37
  • there is no short way to do that with the standard java library. You have to set hour, then minutes, then seconds... Have a look at Joda Time it may suits your needs Commented Nov 20, 2014 at 15:37
  • By inserting into another Date, do you mean adding, removing, or simply creating a Date with just the time? Commented Nov 20, 2014 at 15:38
  • Null out year, month,day fields of your Date instance. essentially what you have is a time. Haven't managed to find easier ways. Commented Nov 20, 2014 at 15:40

4 Answers 4

1

Try this:

public static Date copyTimeOnly(Date toDate, Date fromDate) { Calendar toCal = new GregorianCalendar(); toCal.setTime(toDate); Calendar fromCal = new GregorianCalendar(); fromCal.setTime(fromDate); // Copy time only toCal.set(Calendar.HOUR_OF_DAY, fromCal.get(Calendar.HOUR_OF_DAY)); toCal.set(Calendar.MINUTE, fromCal.get(Calendar.MINUTE)); toCal.set(Calendar.SECOND, fromCal.get(Calendar.SECOND)); toCal.set(Calendar.MILLISECOND, fromCal.get(Calendar.MILLISECOND)); return toCal.getTime(); } 
Sign up to request clarification or add additional context in comments.

Comments

1

If you just need the time part of your Date object and you can't use Java 8 and don't want to use any third party framework, then use Calendar:

Calendar c = Calendar.getInstance(); c.setTime(yourDate); c.set(Calendar.YEAR, 0); c.set(Calendar.MONTH, 0); c.set(Calendar.DAY, 0); Date timeOnly = c.getTime(); 

Comments

0

or if you can use third party libraries(in this case org.apache.commons.lang3.time) you could do it this way:

private Date truncate(Date pDate) { return DateUtils.truncate(pDate, Calendar.DATE); } 

Comments

0

LocalTime

The old java.util.Date/.Calendar have no class to represent a time-of-day value without a date portion. In contrast, both the Joda-Time library and the new java.time package built into Java 8 (inspired by Joda-Time) offer a LocalTime class.

Joda-Time

Here is example code using Joda-Time 2.5.

Time zone is crucial. Do you want the time of day for that moment as seen by someone in Paris, Montréal, or Kolkata?

DateTimeZone zone = DateTimeZone.forID( "America/Montreal" ); DateTime dateTime = new DateTime( yourJUDate, zone ); LocalTime localTime = dateTime.toLocalTime(); 

To apply that LocalTime to another DateTime, call withTime. Joda-Time uses immutable objects. So a new DateTime object is created with values based on the original.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.