Example usage for java.util GregorianCalendar GregorianCalendar

List of usage examples for java.util GregorianCalendar GregorianCalendar

Introduction

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

Prototype

public GregorianCalendar() 

Source Link

Document

Constructs a default GregorianCalendar using the current time in the default time zone with the default Locale.Category#FORMAT FORMAT locale.

Usage

From source file:MyServlet.java

public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
        throws IOException, ServletException {

    GregorianCalendar calendar = new GregorianCalendar();
    Date date1 = new Date();
    calendar.setTime(date1);/*  w  ww  . j  a  v a 2 s  .  c om*/
    int hour = calendar.get(Calendar.HOUR_OF_DAY);
    if (hour < 9 || hour > 17) {
        chain.doFilter(request, response);
    } else {
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        out.println("<HTML>");
        out.println("<HEAD>");
        out.println("<TITLE>");
        out.println("Get Back to Work!");
        out.println("</TITLE>");
        out.println("</HEAD>");
        out.println("<BODY>");
        out.println("<H1>Get Back to Work!</H1>");
        out.println("Sorry, that resource is not available now.");
        out.println("</BODY>");
        out.println("</HTML>");
    }
}

From source file:com.github.jgility.core.util.ReleasePlanningUtils.java

private static Calendar addDayOffset(final Calendar date, long offset) {
    Calendar newDate = new GregorianCalendar();
    newDate.setTimeInMillis(date.getTimeInMillis());
    newDate.add(Calendar.DAY_OF_MONTH, safeLongToInt(offset));
    return newDate;
}

From source file:Main.java

public static String getStringByFormat(String strDate, String format) {
    String mDateTime = null;//from   www  .  j av a  2 s . c om
    try {
        Calendar c = new GregorianCalendar();
        SimpleDateFormat mSimpleDateFormat = new SimpleDateFormat(dateFormatYMDHMS);
        c.setTime(mSimpleDateFormat.parse(strDate));
        SimpleDateFormat mSimpleDateFormat2 = new SimpleDateFormat(format);
        mDateTime = mSimpleDateFormat2.format(c.getTime());
    } catch (Exception e) {
        e.printStackTrace();
    }
    return mDateTime;
}

From source file:Main.java

/**
 * Converts a java.util.Date into an instance of XMLGregorianCalendar
 *
 * @param date Instance of java.util.Date or a null reference
 * @return XMLGregorianCalendar instance whose value is based upon the
 * value in the date parameter. If the date parameter is null then
 * this method will simply return null./*from  w  w  w  .j  a  v a 2 s  .c o  m*/
 */
public static XMLGregorianCalendar dateAsCalendar(Date date) {
    if (date == null) {
        return null;
    } else {
        GregorianCalendar gc = new GregorianCalendar();
        gc.setTimeInMillis(date.getTime());
        return df.newXMLGregorianCalendar(gc);
    }
}

From source file:com.pureinfo.force.util.TimerUtil.java

/**
 * @param _sTime/*  ww w .  j a  va  2 s  .com*/
 *            must be "HH:mm" format
 */
public static Timer scheduleFrom(String _sTime, long _lPeriod, TimerTask _task) {
    Calendar nowTime = new GregorianCalendar();
    Calendar firstTime = getTimeToday(nowTime, _sTime);
    firstTime.setTimeInMillis(getNext(firstTime.getTimeInMillis(), nowTime.getTimeInMillis(), _lPeriod));
    Timer timer = new Timer();
    timer.scheduleAtFixedRate(_task, firstTime.getTime(), _lPeriod);
    return timer;
}

From source file:Main.java

/**
 * Converts a java.util.Date to a javax.xml.datatype.XMLGregorianCalendar, uses current time if specified date is null;
 * // w  w  w . j a v a  2  s  . co  m
 * @param date, uses current time if specified date is null
 * @return a corresponding XMLGregorianCalendar object
 */
public static XMLGregorianCalendar convertDateToXmlDate(Date date) {
    try {
        GregorianCalendar fromDate = new GregorianCalendar();
        if (date != null) {
            fromDate.setTime(date);
        }
        return DatatypeFactory.newInstance().newXMLGregorianCalendar(fromDate);
    } catch (DatatypeConfigurationException e) {
        throw new RuntimeException(e);
    }
}

From source file:Main.java

/**
 * Converts a <code>java.util.Date</code> instance to a
 * <code>javax.xml.datatype.XMLGregorianCalendar</code> instance.
 *
 * @param date the <code>java.util.Date</code> instance to convert
 *
 * @return the converted <code>javax.xml.datatype.XMLGregorianCalendar</code> instance
 *//*ww w.  j  a va  2s.c  om*/
public static XMLGregorianCalendar asXMLGregorianCalendar(java.util.Date date) {
    if (date == null) {
        return null;
    } else {
        GregorianCalendar calendar = new GregorianCalendar();

        calendar.setTimeInMillis(date.getTime());

        return datatypeFactory.newXMLGregorianCalendar(calendar);
    }
}

From source file:DateHelper.java

/**
 * Calculating age from a current date/*  w  w w  . j a  v  a 2  s . c  o  m*/
 * 
 * @param current
 * @param birthdate
 * @return Age from the current (arg) date
 */
public static float getAge(final Date current, final Date birthdate) {

    if (birthdate == null) {
        return 0;
    }
    if (current == null) {
        return getAge(birthdate);
    } else {
        final Calendar calend = new GregorianCalendar();
        calend.set(Calendar.HOUR_OF_DAY, 0);
        calend.set(Calendar.MINUTE, 0);
        calend.set(Calendar.SECOND, 0);
        calend.set(Calendar.MILLISECOND, 0);

        calend.setTimeInMillis(current.getTime() - birthdate.getTime());

        float result = 0;
        result = calend.get(Calendar.YEAR) - 1970;
        result += (float) calend.get(Calendar.MONTH) / (float) 12;
        return result;
    }

}

From source file:Main.java

public static String getStringByOffset(Date date, String format, int calendarField, int offset) {
    String strDate = null;/*w  ww  .  ja  v a 2 s  .  c  o  m*/
    try {
        Calendar c = new GregorianCalendar();
        SimpleDateFormat mSimpleDateFormat = new SimpleDateFormat(format);
        c.setTime(date);
        c.add(calendarField, offset);
        strDate = mSimpleDateFormat.format(c.getTime());
    } catch (Exception e) {
        e.printStackTrace();
    }
    return strDate;
}

From source file:Main.java

/**
 * Converts a lexical duration, as defined by XML Schema 1.0, into milliseconds.
 * /* w  ww  .ja  va 2s.  co m*/
 * @param duration lexical duration representation
 * 
 * @return duration in milliseconds
 */
public static long durationToLong(String duration) {
    Duration xmlDuration = getDataTypeFactory().newDuration(duration);
    return xmlDuration.getTimeInMillis(new GregorianCalendar());
}