Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set time to a date object in java

I created a Date object in Java. When I do so, it shows something like: date=Tue Aug 09 00:00:00 IST 2011. As a result, it appears that my Excel file is lesser by one day (27 feb becomes 26 feb and so on) I think it must be because of time. How can I set it to something like 5:30 pm?

like image 885
CyprUS Avatar asked Mar 02 '11 09:03

CyprUS


People also ask

How do you set a specific time in Java?

The setTime() method of Java Date class sets a date object. It sets date object to represent time milliseconds after January 1, 1970 00:00:00 GMT. Parameters: The function accepts a single parameter time which specifies the number of milliseconds. Return Value: It method has no return value.

How do you create a date and time object in Java?

You can create a Date object using the Date() constructor of java. util. Date constructor as shown in the following example. The object created using this constructor represents the current time.

How do I change TimeZone to Java Util date?

You can make use of the following DateFormat. SimpleDateFormat myDate = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss"); myDate. setTimeZone(TimeZone. getTimeZone("UTC")); Date newDate = myDate.

Does Java date include time?

No time data is kept. In fact, the date is stored as milliseconds since the 1st of January 1970 00:00:00 GMT and the time part is normalized, i.e. set to zero. Basically, it's a wrapper around java. util.


1 Answers

Calendar cal = Calendar.getInstance(); cal.set(Calendar.HOUR_OF_DAY,17); cal.set(Calendar.MINUTE,30); cal.set(Calendar.SECOND,0); cal.set(Calendar.MILLISECOND,0);  Date d = cal.getTime(); 

Also See

  • Joda time
  • Calendar doc
like image 51
jmj Avatar answered Oct 06 '22 01:10

jmj