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:com.livinglogic.ul4.FunctionAsJSON.java

private static void call(StringBuilder builder, Object obj) {
    if (obj == null)
        builder.append("null");
    else if (obj instanceof Boolean)
        builder.append(((Boolean) obj).booleanValue() ? "true" : "false");
    else if (obj instanceof Integer || obj instanceof Byte || obj instanceof Short || obj instanceof Long
            || obj instanceof BigInteger || obj instanceof Double || obj instanceof Float)
        builder.append(obj.toString());/* w  ww .  j a v  a  2 s.c om*/
    else if (obj instanceof BigDecimal) {
        String result = obj.toString();
        builder.append(result);
        if (result.indexOf('.') < 0 || result.indexOf('E') < 0 || result.indexOf('e') < 0)
            builder.append(".0");
    } else if (obj instanceof String)
        builder.append("\"")
                // We're using StringEscapeUtils.escapeJava() here, which is the same as escapeJavaScript, except that it doesn't escape ' (which is illegal in JSON strings according to json.org)
                .append(StringEscapeUtils.escapeJava(((String) obj))).append("\"");
    else if (obj instanceof Date) {
        Calendar calendar = new GregorianCalendar();
        calendar.setTime((Date) obj);
        builder.append("new Date(").append(calendar.get(Calendar.YEAR)).append(", ")
                .append(calendar.get(Calendar.MONTH)).append(", ").append(calendar.get(Calendar.DAY_OF_MONTH))
                .append(", ").append(calendar.get(Calendar.HOUR_OF_DAY)).append(", ")
                .append(calendar.get(Calendar.MINUTE)).append(", ").append(calendar.get(Calendar.SECOND));
        int milliSeconds = calendar.get(Calendar.MILLISECOND);
        if (milliSeconds != 0) {
            builder.append(", ").append(milliSeconds);
        }
        builder.append(")");
    } else if (obj instanceof InterpretedTemplate) {
        builder.append("ul4.Template.loads(\"")
                .append(StringEscapeUtils.escapeJavaScript(((InterpretedTemplate) obj).dumps())).append("\")");
    } else if (obj instanceof TemplateClosure) {
        builder.append("ul4.Template.loads(\"")
                .append(StringEscapeUtils.escapeJavaScript(((TemplateClosure) obj).getTemplate().dumps()))
                .append("\")");
    } else if (obj instanceof UL4Attributes) {
        builder.append("{");
        boolean first = true;
        Set<String> attributeNames = ((UL4Attributes) obj).getAttributeNamesUL4();
        for (String attributeName : attributeNames) {
            if (first)
                first = false;
            else
                builder.append(", ");
            call(builder, attributeName);
            builder.append(": ");
            Object value = ((UL4Attributes) obj).getItemStringUL4(attributeName);
            call(builder, value);
        }
        builder.append("}");
    } else if (obj instanceof Color) {
        Color c = (Color) obj;
        builder.append("ul4.Color.create(").append(c.getR()).append(", ").append(c.getG()).append(", ")
                .append(c.getB()).append(", ").append(c.getA()).append(")");
    } else if (obj instanceof Collection) {
        builder.append("[");
        boolean first = true;
        for (Object o : (Collection) obj) {
            if (first)
                first = false;
            else
                builder.append(", ");
            call(builder, o);
        }
        builder.append("]");
    } else if (obj instanceof Object[]) {
        builder.append("[");
        boolean first = true;
        for (Object o : (Object[]) obj) {
            if (first)
                first = false;
            else
                builder.append(", ");
            call(builder, o);
        }
        builder.append("]");
    } else if (obj instanceof Map) {
        builder.append("{");
        boolean first = true;
        Set<Map.Entry> entrySet = ((Map) obj).entrySet();
        for (Map.Entry entry : entrySet) {
            if (first)
                first = false;
            else
                builder.append(", ");
            call(builder, entry.getKey());
            builder.append(": ");
            call(builder, entry.getValue());
        }
        builder.append("}");
    }
}

From source file:com.sammyun.util.DateUtil.java

/**
 * ?/* w  w  w  .  j  a  v a 2 s  .c  o m*/
 * 
 * @return int
 */
public static int getCurrYear() {
    return new GregorianCalendar().get(Calendar.YEAR);
}

From source file:TimeUtil.java

/**
 * convert time in milliseconds into a display string of the form [h]h:mm
 * [am|pm] (traditional) or hh:mm (24 hour format) if using traditional
 * format, the leading 'h' & 'm' will be padded with a space to ensure
 * constant length if less than 10 24 hour format
 * /*from w w w  .j a v  a 2  s . c o  m*/
 * @param msecs
 *            a millisecond time
 * @return TimeString the formatted time string
 */
public static String stringFormat(long msecs) {
    GregorianCalendar cal = new GregorianCalendar();
    StringBuffer sBuf = new StringBuffer(8);

    cal.setTime(new Date(msecs));

    int hour = cal.get(Calendar.HOUR);

    if (hour == 0)
        hour = 12;

    if (hour < 10)
        sBuf.append(" ");

    sBuf.append(Integer.toString(hour));
    sBuf.append(":");

    int minute = cal.get(Calendar.MINUTE);

    if (minute < 10)
        sBuf.append("0");

    sBuf.append(Integer.toString(minute));
    sBuf.append(" ");
    sBuf.append(cal.get(Calendar.AM_PM) == Calendar.AM ? "AM" : "PM");

    return (sBuf.toString());
}

From source file:com.qpark.eip.core.spring.statistics.dao.StatisticsEraser.java

/**
 * Each day at 0:00 remove old out dated entries.
 *//*from  w ww .  ja  va  2 s .c o  m*/
@Scheduled(cron = "0 0 0 * * *")
public void erase() {
    Calendar gc = new GregorianCalendar();
    gc.set(Calendar.HOUR_OF_DAY, 0);
    gc.set(Calendar.MINUTE, 0);
    gc.set(Calendar.SECOND, 0);
    gc.set(Calendar.MILLISECOND, 0);
    gc.add(Calendar.WEEK_OF_YEAR, -1 * Math.abs(this.numberOfWeeksToKeepLogs));
    this.dao.eraseSystemUserLog(gc.getTime());
    this.dao.eraseApplicationUserLog(gc.getTime());
    this.dao.eraseFlowLogMessage(gc.getTime());
}

From source file:com.thed.zephyr.jenkins.utils.rest.Cycle.java

public static Long createCycle(ZephyrConfigModel zephyrData) {

    Long cycleId = 0L;/*from w  w  w . java2 s . c  om*/

    HttpResponse response = null;
    try {
        String createCycleURL = URL_CREATE_CYCLES.replace("{SERVER}", zephyrData.getRestClient().getUrl());

        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("E dd, yyyy hh:mm a");
        String dateFormatForCycleCreation = sdf.format(date);

        JSONObject jObject = new JSONObject();
        String cycleName = zephyrData.getCyclePrefix() + dateFormatForCycleCreation;

        SimpleDateFormat sdf1 = new SimpleDateFormat("dd/MMM/yy");
        String startDate = sdf1.format(date);

        GregorianCalendar gCal = new GregorianCalendar();

        if (zephyrData.getCycleDuration().trim().equalsIgnoreCase("30 days")) {
            gCal.add(Calendar.DAY_OF_MONTH, +29);
        } else if (zephyrData.getCycleDuration().trim().equalsIgnoreCase("7 days")) {
            gCal.add(Calendar.DAY_OF_MONTH, +6);
        }

        String endDate = sdf1.format(gCal.getTime());

        jObject.put("name", cycleName);
        jObject.put("projectId", zephyrData.getZephyrProjectId());
        jObject.put("versionId", zephyrData.getVersionId());
        jObject.put("startDate", startDate);
        jObject.put("endDate", endDate);

        StringEntity se = new StringEntity(jObject.toString(), "utf-8");

        HttpPost createCycleRequest = new HttpPost(createCycleURL);

        createCycleRequest.setHeader("Content-Type", "application/json");
        createCycleRequest.setEntity(se);
        response = zephyrData.getRestClient().getHttpclient().execute(createCycleRequest,
                zephyrData.getRestClient().getContext());
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    int statusCode = response.getStatusLine().getStatusCode();

    if (statusCode >= 200 && statusCode < 300) {
        HttpEntity entity = response.getEntity();
        String string = null;
        try {
            string = EntityUtils.toString(entity);
        } catch (ParseException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        try {
            JSONObject cycleObj = new JSONObject(string);
            cycleId = cycleObj.getLong("id");

        } catch (JSONException e) {
            e.printStackTrace();
        }

    } else if (statusCode == 405) {

        try {
            throw new ClientProtocolException("ZAPI plugin license is invalid" + statusCode);
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        }

    } else {
        try {
            throw new ClientProtocolException("Unexpected response status: " + statusCode);
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        }
    }

    return cycleId;
}

From source file:com.hp.hpl.jena.sparql.util.Utils.java

public static String todayAsXSDDateString() {
    return calendarToXSDDateString(new GregorianCalendar());
}

From source file:com.aw.core.db.DbUtil.java

public boolean hasOnlyDateInfo(Date date) {
    Calendar cal = new GregorianCalendar();
    cal.setTime(date);/*from www  . ja  v a  2  s  .c  o  m*/
    if (cal.get(Calendar.HOUR_OF_DAY) != 0)
        return false;
    if (cal.get(Calendar.MINUTE) != 0)
        return false;
    if (cal.get(Calendar.SECOND) != 0)
        return false;
    if (cal.get(Calendar.MILLISECOND) != 0)
        return false;
    return true;
}

From source file:DateUtils.java

public static final String dateToString(Date dt, String dateformat) {
    GregorianCalendar cal = new GregorianCalendar();
    cal.setTime(dt);//from   w w w . ja  v a2  s .c o m

    StringBuffer ret = new StringBuffer();
    String separator = new String();
    if (dateformat.equals(DateUtils.FORMAT_YYYYMMDD)) {
        separator = "-";
    }
    if (dateformat.equals(DateUtils.FORMAT_YYYYMMDD_SLASHES)) {
        separator = "/";
    }
    ret.append(cal.get(Calendar.YEAR));
    ret.append(separator);
    ret.append(cal.get(Calendar.MONTH) + 1);
    ret.append(separator);
    ret.append(cal.get(Calendar.DATE));

    return ret.toString();
}

From source file:TimeUtil.java

public static String dayStringFormat(long msecs) {
    GregorianCalendar cal = new GregorianCalendar();

    cal.setTime(new Date(msecs));

    int dow = cal.get(Calendar.DAY_OF_WEEK);

    switch (dow) {
    case Calendar.MONDAY:
        return "Monday";
    case Calendar.TUESDAY:
        return "Tuesday";
    case Calendar.WEDNESDAY:
        return "Wednesday";
    case Calendar.THURSDAY:
        return "Thursday";
    case Calendar.FRIDAY:
        return "Friday";
    case Calendar.SATURDAY:
        return "Saturday";
    case Calendar.SUNDAY:
        return "Sunday";
    }/*  w w w . ja  va 2 s.com*/

    return "Unknown";
}

From source file:DateUtils.java

/**
 * Get unix style date string.// w  w w. jav  a 2s  .c  om
 */
public final static String getUnixDate(long millis) {
    if (millis < 0) {
        return "------------";
    }

    StringBuffer sb = new StringBuffer(16);
    Calendar cal = new GregorianCalendar();
    cal.setTimeInMillis(millis);

    // month
    sb.append(MONTHS[cal.get(Calendar.MONTH)]);
    sb.append(' ');

    // day
    int day = cal.get(Calendar.DATE);
    if (day < 10) {
        sb.append(' ');
    }
    sb.append(day);
    sb.append(' ');

    long sixMonth = 15811200000L; // 183L * 24L * 60L * 60L * 1000L;
    long nowTime = System.currentTimeMillis();
    if (Math.abs(nowTime - millis) > sixMonth) {

        // year
        int year = cal.get(Calendar.YEAR);
        sb.append(' ');
        sb.append(year);
    } else {

        // hour
        int hh = cal.get(Calendar.HOUR_OF_DAY);
        if (hh < 10) {
            sb.append('0');
        }
        sb.append(hh);
        sb.append(':');

        // minute
        int mm = cal.get(Calendar.MINUTE);
        if (mm < 10) {
            sb.append('0');
        }
        sb.append(mm);
    }
    return sb.toString();
}