Example usage for java.util GregorianCalendar setTime

List of usage examples for java.util GregorianCalendar setTime

Introduction

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

Prototype

public final void setTime(Date date) 

Source Link

Document

Sets this Calendar's time with the given Date.

Usage

From source file:org.tsm.concharto.util.TimeRangeFormat.java

/** 
 * format a date//ww w . ja v  a 2 s . c  om
 * @param date date
 * @param cp CalendarPrecision for getting one of the format strings
 * @return String formatted string
 */
private static String dateFormat(Date date, CalendarPrecision cp) {
    String format;
    GregorianCalendar cal = new GregorianCalendar();
    cal.setTime(date);
    //if the year is an AD date and it is pretty old (e.g. less than 1000AD), then append the era
    //always display the era for BC dates
    if ((cal.get(Calendar.ERA) == GregorianCalendar.BC) || (cal.get(Calendar.YEAR) < MAX_YEAR_TO_DISLPAY_ERA)) {
        format = cp.getFormatWithEra();
    } else {
        format = cp.getFormat();
    }

    String text = DateFormatUtils.format(date, format);
    return stripLeadingZeros(date, text);
}

From source file:org.openflamingo.uploader.util.DateUtils.java

/**
 * java.util.Date  java.util.GregorianCalendar  .
 *
 * @param date //  w  ww  .  jav a 2 s .com
 * @return GregorianCalendar
 */
public static java.util.GregorianCalendar dateToGregorianCalendar(Date date) {
    if (date == null)
        return null;
    GregorianCalendar cal = new GregorianCalendar(TimeZone.getTimeZone("GMT+09:00"), Locale.KOREAN);
    cal.setTime(date);
    return cal;
}

From source file:org.tsm.concharto.util.TimeRangeFormat.java

/**
 * Make formatted date more user friendly.  For example, '0092 BC' is converted
 * to '92 BC'//  www.j  a  v a  2  s  .  co m
 * 
 * @param date original date
 * @param text formatted text representation of that date
 * @return stripped text
 */
private static String stripLeadingZeros(Date date, String text) {
    //first we have to find the formatted year.
    GregorianCalendar cal = new GregorianCalendar();
    cal.setTime(date);
    String year = Integer.toString(cal.get(Calendar.YEAR));
    //only do this for four digit years (e.g. no need to do it for 20000 BC)
    if (year.length() <= DIGITS_IN_YEAR) {
        //find the start pos of the year.  If the year is 0093, we will get
        //year = '93' so the start of year = pos of '93' minus 2.
        int yearPos = text.indexOf(year) - DIGITS_IN_YEAR + year.length();
        StringBuffer adjusted = new StringBuffer();
        if (yearPos != 0) {
            adjusted.append(text.substring(0, yearPos));
        }
        adjusted.append(year);
        adjusted.append(text.substring(yearPos + DIGITS_IN_YEAR, text.length()));
        return adjusted.toString();
    } else {
        return text;
    }
}

From source file:bioLockJ.AppController.java

private static File getRestartDir() throws Exception {
    File restartDir = null;//  ww w.j  ava2 s .c  o m
    GregorianCalendar mostRecent = null;
    final FileFilter ff = new WildcardFileFilter(Config.requireString(Config.PROJECT_NAME) + "*");
    final File[] dirs = Config.requireExistingDirectory(PROJECTS_DIR).listFiles(ff);

    for (final File d : dirs) {
        if (!d.isDirectory()
                || (d.getName().length() != (Config.requireString(Config.PROJECT_NAME).length() + 10))) {
            continue;
        }

        final String name = d.getName();
        final int len = name.length();
        final String year = name.substring(len - 9, len - 5);
        final String mon = name.substring(len - 5, len - 2);
        final String day = name.substring(len - 2);
        final Date date = new SimpleDateFormat("yyyyMMMdd").parse(year + mon + day);
        final GregorianCalendar projectDate = new GregorianCalendar();
        projectDate.setTime(date);

        // Value > 0 if projectDate has a more recent date than mostRecent
        if ((mostRecent == null) || (projectDate.compareTo(mostRecent) > 0)) {
            Log.addMsg("Found previous run = " + d.getAbsolutePath());
            restartDir = d;
            mostRecent = projectDate;
        }
    }

    if (restartDir == null) {
        throw new Exception(
                "Unalbe to locate restart directory in --> " + Config.requireExistingDirectory(PROJECTS_DIR));
    }

    if (isProjectComplete(restartDir)) {
        throw new Exception("RESTART FAILED!  Project ran successfully: " + restartDir.getAbsolutePath());
    }

    Log.addMsg(Constants.RETURN);
    Log.addMsg(Constants.RETURN);
    Log.addMsg(Constants.LOG_SPACER);
    Log.addMsg(Constants.LOG_SPACER);
    Log.addMsg(Constants.RETURN);
    Log.addMsg("RESTART PROJECT DIR --> " + restartDir.getAbsolutePath());
    Log.addMsg(Constants.RETURN);
    Log.addMsg(Constants.LOG_SPACER);
    Log.addMsg(Constants.LOG_SPACER);
    Log.addMsg(Constants.RETURN);

    return restartDir;
}

From source file:com.evolveum.midpoint.prism.xml.XmlTypeConverter.java

public static XMLGregorianCalendar createXMLGregorianCalendar(Date date) {
    if (date == null) {
        return null;
    }//from   w  w  w  .  ja v  a 2s. c o m
    GregorianCalendar gregorianCalendar = new GregorianCalendar();
    gregorianCalendar.setTime(date);
    return createXMLGregorianCalendar(gregorianCalendar);
}

From source file:util.android.util.DateUtils.java

public static int getAge(Date dateOfBirth) {
    GregorianCalendar cal = new GregorianCalendar();
    int y, m, d, a;

    y = cal.get(Calendar.YEAR);/*from  ww w. jav  a  2s.c o m*/
    m = cal.get(Calendar.MONTH);
    d = cal.get(Calendar.DAY_OF_MONTH);

    cal.setTime(dateOfBirth);

    a = y - cal.get(Calendar.YEAR);
    if ((m < cal.get(Calendar.MONTH))
            || ((m == cal.get(Calendar.MONTH)) && (d < cal.get(Calendar.DAY_OF_MONTH)))) {
        --a;
    }
    if (a < 0)
        throw new IllegalArgumentException("Age < 0");
    return a;
}

From source file:org.novoj.utils.datePattern.DatePatternConverter.java

/**
 * Method returns date object with normalized date:
 * - when pattern doesn't contain year pattern -> current year is used
 * - when pattern doesn't contain month pattern
 *    date represents current year -> current month is used
 *                        else -> january is used
 * - when pattern doesn't contain day pattern
 *    date represents current year and month -> current day is used
 *                                 else -> first day of month is used
 *///from   w  w w  . j  av  a2s.c  om
private static Date normalizeDate(Calendar nowCld, Date date, Locale locale, String pattern) {
    GregorianCalendar dateCld = new GregorianCalendar();
    dateCld.setTime(date);
    //set current date
    if (pattern.toLowerCase(locale).indexOf('y') == -1) {
        dateCld.set(Calendar.YEAR, nowCld.get(Calendar.YEAR));
    }

    if (pattern.indexOf('M') == -1) {
        if (nowCld.get(Calendar.YEAR) == dateCld.get(Calendar.YEAR)) {
            dateCld.set(Calendar.MONTH, nowCld.get(Calendar.MONTH));
        } else {
            dateCld.set(Calendar.MONTH, 0);
        }
    }
    if (pattern.toLowerCase(locale).indexOf('d') == -1) {
        if (nowCld.get(Calendar.YEAR) != dateCld.get(Calendar.YEAR)
                || nowCld.get(Calendar.MONTH) != dateCld.get(Calendar.MONTH)) {
            dateCld.set(Calendar.DAY_OF_MONTH, 1);
        } else {
            dateCld.set(Calendar.DAY_OF_MONTH, nowCld.get(Calendar.DAY_OF_MONTH));
        }
    }
    return dateCld.getTime();
}

From source file:com.inmobi.grill.server.metastore.JAXBUtils.java

public static XMLGregorianCalendar getXMLGregorianCalendar(Date d) {
    if (d == null) {
        return null;
    }/*from w w w.j  ava2 s. com*/

    GregorianCalendar c1 = new GregorianCalendar();
    c1.setTime(d);
    try {
        return DatatypeFactory.newInstance().newXMLGregorianCalendar(c1);
    } catch (DatatypeConfigurationException e) {
        LOG.warn("Error converting date " + d, e);
        return null;
    }
}

From source file:io.uengine.util.DateUtils.java

/**
 * java.util.Date  java.util.GregorianCalendar  .
 *
 * @param date //ww w .  j  a  va 2  s .c om
 * @return GregorianCalendar
 */
public static GregorianCalendar dateToGregorianCalendar(Date date) {
    if (date == null)
        return null;
    GregorianCalendar cal = new GregorianCalendar(TimeZone.getTimeZone("GMT+09:00"), Locale.KOREAN);
    cal.setTime(date);
    return cal;
}

From source file:org.novoj.utils.datePattern.DatePatternConverter.java

/**
 * Method returns date object with normalized time. When resetTime = true, time will be set to 0:0:0.0.
 * When date respresents current day (current date) and pattern doesn't contain appropriate pattern parts for
 * hour, minute, second, milliseconds - those values will be set to current time values, otherwise they will be
 * set to initial values (ie. 0).//from  ww w.jav a 2s  .  c  om
 */
private static Date normalizeHours(Calendar nowCld, Date date, Locale locale, String pattern,
        boolean resetTime) {
    GregorianCalendar dateCld = new GregorianCalendar();
    dateCld.setTime(date);
    boolean currentYear = nowCld.get(Calendar.YEAR) == dateCld.get(Calendar.YEAR);
    boolean currentMonth = nowCld.get(Calendar.MONTH) == dateCld.get(Calendar.MONTH);
    boolean currentDay = nowCld.get(Calendar.DAY_OF_MONTH) == dateCld.get(Calendar.DAY_OF_MONTH);
    //noinspection OverlyComplexBooleanExpression
    if (!resetTime && currentYear && currentMonth && currentDay) {
        //set current time
        if (pattern.toLowerCase(locale).indexOf('h') == -1) {
            dateCld.set(Calendar.HOUR, nowCld.get(Calendar.HOUR));
        }
        if (pattern.indexOf('m') == -1) {
            dateCld.set(Calendar.MINUTE, nowCld.get(Calendar.MINUTE));
        }
        if (pattern.indexOf('s') == -1) {
            dateCld.set(Calendar.SECOND, nowCld.get(Calendar.SECOND));
        }
        if (pattern.indexOf('S') == -1) {
            dateCld.set(Calendar.MILLISECOND, nowCld.get(Calendar.MILLISECOND));
        }
    } else {
        //set zero time
        if (pattern.toLowerCase(locale).indexOf('h') == -1) {
            dateCld.set(Calendar.HOUR, 0);
        }
        if (pattern.indexOf('m') == -1) {
            dateCld.set(Calendar.MINUTE, 0);
        }
        if (pattern.indexOf('s') == -1) {
            dateCld.set(Calendar.SECOND, 0);
        }
        if (pattern.indexOf('S') == -1) {
            dateCld.set(Calendar.MILLISECOND, 0);
        }
    }
    return dateCld.getTime();
}