Skip to main content

APIzation: Replication Package

[Q#5165428][A#5165471] How to set time to a date object in java

https://stackoverflow.com/q/5165428

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?

Answer

https://stackoverflow.com/a/5165471

Also See

APIzation

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();
package com.stackoverflow.api;

import java.util.Calendar;
import java.util.Date;

public class Human5165471 {

  public static Date dateFromTime(
    int hour,
    int minute,
    int second,
    int millisecond
  ) {
    Calendar cal = Calendar.getInstance();
    cal.set(Calendar.HOUR_OF_DAY, hour);
    cal.set(Calendar.MINUTE, minute);
    cal.set(Calendar.SECOND, second);
    cal.set(Calendar.MILLISECOND, millisecond);
    return cal.getTime();
  }
}

package com.stackoverflow.api;

import java.util.Calendar;
import java.util.Date;

/**
 * How to set time to a date object in java
 *
 * @author APIzator
 * @see <a href="https://stackoverflow.com/a/5165471">https://stackoverflow.com/a/5165471</a>
 */
public class APIzator5165471 {

  public static Date setTime() throws Exception {
    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);
    return cal.getTime();
  }
}