Example usage for java.util Calendar setTime

List of usage examples for java.util Calendar setTime

Introduction

In this page you can find the example usage for java.util Calendar setTime.

Prototype

public final void setTime(Date date) 

Source Link

Document

Sets this Calendar's time with the given Date.

Usage

From source file:gob.dp.simco.comun.FunctionUtil.java

public static Integer calcularMesesAFecha(Date fechaInicio, Date fechaFin) {
    try {/*from  w w  w . j  a  va2s  .  c  o m*/
        //Fecha inicio en objeto Calendar
        Calendar startCalendar = Calendar.getInstance();
        startCalendar.setTime(fechaInicio);
        //Fecha finalizacin en objeto Calendar
        Calendar endCalendar = Calendar.getInstance();
        endCalendar.setTime(fechaFin);
        //Clculo de meses para las fechas de inicio y finalizacin
        int startMes = (startCalendar.get(Calendar.YEAR) * 12) + startCalendar.get(Calendar.MONTH);
        int endMes = (endCalendar.get(Calendar.YEAR) * 12) + endCalendar.get(Calendar.MONTH);
        //Diferencia en meses entre las dos fechas
        int diffMonth = endMes - startMes;
        //Si la el da de la fecha de finalizacin es menor que el da de la fecha inicio
        //se resta un mes, puesto que no estaria cumpliendo otro periodo.
        //Para esto ocupo el metoddo ponerAnioMesActual
        Date aFecha = ponerAnioMesActual(fechaInicio, fechaFin).getTime();
        if (formatearDate(fechaFin, "dd/MM/yyyy").compareTo(formatearDate(aFecha, "dd/MM/yyyy")) < 0) {
            diffMonth = diffMonth - 1;
        }
        //Si la fecha de finalizacin es menor que la fecha de inicio, retorno que los meses
        // transcurridos entre las dos fechas es 0
        if (diffMonth < 0) {
            diffMonth = 0;
        }
        return diffMonth;
    } catch (Exception e) {
        return 0;
    }
}

From source file:org.openmrs.module.patientportaltoolkit.api.db.hibernate.HibernateReminderDAO.java

public static Date clearDate(Date dateTime) {
    if (dateTime == null) {
        return null;
    }/*from ww w  . ja v  a2 s .  co m*/
    Calendar cal = Calendar.getInstance();
    cal.setTime(dateTime);
    cal.set(Calendar.HOUR_OF_DAY, 0);
    cal.set(Calendar.MINUTE, 0);
    cal.set(Calendar.SECOND, 0);
    cal.set(Calendar.MILLISECOND, 0);
    Date dateWithoutTime = cal.getTime();

    return dateWithoutTime;
}

From source file:com.redhat.rhn.domain.common.CommonFactory.java

/**
 * Create a TinyUrl//w  w  w .  ja v a  2 s  .co m
 * @param urlIn to tinyfy
 * @param expires the date we *ADD* 6 hours to to set the expiration on the URL
 * @return TinyUrl instance
 */
public static TinyUrl createTinyUrl(String urlIn, Date expires) {
    String token = RandomStringUtils.randomAlphanumeric(8);
    TinyUrl existing = lookupTinyUrl(token);
    while (existing != null) {
        log.warn("Had collision with: " + token);
        token = RandomStringUtils.randomAlphanumeric(8);
        existing = lookupTinyUrl(token);
    }

    TinyUrl url = new TinyUrl();
    Config c = new Config();
    url.setUrl(urlIn);
    url.setEnabled(true);
    url.setToken(token);
    Calendar pcal = Calendar.getInstance();
    pcal.setTime(expires);
    pcal.add(Calendar.HOUR, c.getInt("server.satellite.tiny_url_timeout", 4));
    url.setExpires(new Date(pcal.getTimeInMillis()));
    return url;
}

From source file:com.vangent.hieos.xutil.hl7.date.Hl7Date.java

/**
 * Convert a Java date to HL7 format.//w  w  w  . j  a  v a 2  s. c o m
 *
 * @param date Java date.
 * @return String In HL7 format.
 */
static public String toHL7format(Date date) {
    String hl7DateFormat = "yyyyMMdd";
    SimpleDateFormat formatter = new SimpleDateFormat(hl7DateFormat);
    Calendar c = Calendar.getInstance();
    c.setTime(date);
    formatter.setTimeZone(TimeZone.getTimeZone("UTC"));

    String hl7formattedDate = formatter.format(date);
    hl7formattedDate.replaceAll("UTC", "Z");
    return hl7formattedDate;
}

From source file:com.alcatel_lucent.nz.wnmsextract.reader.CSVReader.java

/**
 * String tokeniser method will split up a CSV file by row and column 
 * returning a List<List<String>>
 * @param in// w  ww .  ja  v a2s  .co  m
 * @param struct
 * @return
 */
public static ArrayList<ArrayList<String>> read(BufferedReader in, ArrayList<ColumnStructure> struct) {
    ArrayList<ArrayList<String>> dmap = new ArrayList<ArrayList<String>>();
    CSVParser parser = new CSVParser(in, CSVReader.strategy);

    //if header
    try {
        //[consume header]
        //String[] header = 
        parser.getLine();

        //and body
        String[] line = null;

        while ((line = parser.getLine()) != null) {
            ArrayList<String> list = new ArrayList<String>();

            for (int i = 0; i < line.length; i++) {
                ColumnStructure cs = struct.get(i);
                switch (cs) {
                case VC:
                    list.add(line[i]);
                    break;
                case TS:
                    Calendar cal = Calendar.getInstance();
                    cal.setTime(DATA_DF.parse(line[i]));
                    list.add(ALUDBUtilities.ALUDB_DF.format(cal.getTime()));
                    break;
                case FL:
                    list.add(String.valueOf(validateFloat(Float.parseFloat(line[i]))));
                    break;
                case IT:
                    list.add(String.valueOf(validateInt(Integer.parseInt(line[i]))));
                    break;
                default:
                    list.add(line[i]);
                }
            }

            dmap.add(list);
        }
    } catch (IOException ioe) {
        // TODO Auto-generated catch block
        ioe.printStackTrace();
    } catch (ParseException pe) {
        // TODO Auto-generated catch block
        pe.printStackTrace();
    }

    return dmap;
}

From source file:com.rmn.qa.AutomationUtils.java

/**
 * Modifies the specified date//from   w  ww  .  j  a  va 2s .c o m
 * @param dateToModify Date to modify
 * @param unitsToModify Number of units to modify (e.g. 6 for 6 seconds)
 * @param unitType Measurement type (e.g. Calendar.SECONDS)
 * @return Modified date
 */
public static Date modifyDate(Date dateToModify, int unitsToModify, int unitType) {
    Calendar c = Calendar.getInstance();
    c.setTime(dateToModify);
    // Add 60 seconds so we're as close to the hour as we can be instead of adding 55 again
    c.add(unitType, unitsToModify);
    return c.getTime();
}

From source file:net.ceos.project.poi.annotated.bean.cascade.CascadeObjectBuilder.java

/**
 * Validate the CascadeObject based on the object build with the method
 * 'buildCascadeObject'//from  w  w w  .j a  v a  2s .  co m
 * 
 * @param toValidate
 *            the object to validate
 */
public static void validateCascadeObject(CascadeObject toValidate) {
    CascadeObject base = buildCascadeObject();

    Calendar calendar = Calendar.getInstance();
    calendar.setTime(new Date());

    Calendar calendarUnmarshal = Calendar.getInstance();
    calendarUnmarshal.setTime(toValidate.getDateAttribute());
    assertEquals(calendar.get(Calendar.YEAR), calendarUnmarshal.get(Calendar.YEAR));
    assertEquals(calendar.get(Calendar.MONTH), calendarUnmarshal.get(Calendar.MONTH));
    assertEquals(calendar.get(Calendar.DAY_OF_MONTH), calendarUnmarshal.get(Calendar.DAY_OF_MONTH));

    assertEquals(base.getStringAttribute(), toValidate.getStringAttribute());
    assertEquals(base.getIntegerAttribute(), toValidate.getIntegerAttribute());
    assertEquals(base.getDoubleAttribute(), toValidate.getDoubleAttribute());
    assertEquals(base.getLongAttribute(), toValidate.getLongAttribute());
    assertEquals(base.getBooleanAttribute(), toValidate.getBooleanAttribute());
    assertEquals(base.getJob().getJobCode(), toValidate.getJob().getJobCode());
    assertEquals(base.getJob().getJobFamily(), toValidate.getJob().getJobFamily());
    assertEquals(base.getJob().getJobName(), toValidate.getJob().getJobName());

    assertEquals(base.getBasicObjectList(), toValidate.getBasicObjectList());
    assertEquals(base.getObjectConfList(), toValidate.getObjectConfList());
    // TODO add new validation below
}

From source file:gemlite.core.common.DateUtil.java

/**
 * /*from w w  w  .  j  ava  2  s . co m*/
 * @return
 */
public static long today() {
    Calendar c = Calendar.getInstance();
    c.setTime(new Date());
    c.set(Calendar.SECOND, 0);
    c.set(Calendar.MILLISECOND, 0);
    c.set(Calendar.MINUTE, 0);
    c.set(Calendar.HOUR_OF_DAY, 0);
    return c.getTimeInMillis();
}

From source file:de.kurashigegollub.dev.gcatest.Utils.java

public static DateTime getDateTimeNow() {
    Calendar c = Calendar.getInstance();
    c.setTime(new Date());
    c.set(Calendar.HOUR_OF_DAY, 0);
    c.set(Calendar.MINUTE, 0);// w w  w.j  a  v a 2 s  . c  om
    c.set(Calendar.SECOND, 0);
    c.set(Calendar.MILLISECOND, 0);
    //Google's DateTime class works with GMT timezone internally and converts all passed in Date values to it.
    //see code here:
    //http://code.google.com/p/google-api-java-client/source/browse/google-api-client/src/main/java/com/google/api/client/util/DateTime.java?spec=svnf7334c6f6f7c0941306e18de989c90a053941669&r=f7334c6f6f7c0941306e18de989c90a053941669
    //Therefore we have to apply our local timezone here so the calculated time will
    DateTime dtg = new DateTime(c.getTime(), TimeZone.getDefault());
    //        log.info("TimeZone: " + c.getTimeZone().getDisplayName());
    //        log.info("TimeZone: " + c.getTimeZone().getID());
    //        log.info("Date: " + getDateAsString(c.getTime()));
    //        log.info("DateTime: " + getDateAsString(dtg));
    //        log.info("DateTime: " + dtg.toString());
    //        log.info("DateTime: " + dtg.toStringRfc3339());
    return dtg;
}

From source file:com.collabnet.ccf.core.utils.DateUtil.java

/**
 * Converts the given Date object into another date object with the
 * specified time zone information./*from  w  ww  .  j  ava  2 s.c om*/
 * 
 * @param date
 *            - The date that is to be converted to a different time zone.
 * @param toTimeZone
 *            - the time zone to which the date object should be converted.
 * @return the new Date object in the specified time zone.
 * @throws ParseException
 */
public static Date convertDate(Date date, String toTimeZone) throws ParseException {
    Calendar cal = new GregorianCalendar(TimeZone.getTimeZone(toTimeZone));
    cal.setTime(date);
    DateFormat df = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss.SSS Z");
    df.setCalendar(cal);
    return df.parse(df.format(cal.getTime()));
}