Example usage for java.util Calendar ERA

List of usage examples for java.util Calendar ERA

Introduction

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

Prototype

int ERA

To view the source code for java.util Calendar ERA.

Click Source Link

Document

Field number for get and set indicating the era, e.g., AD or BC in the Julian calendar.

Usage

From source file:Main.java

/**
 * Checks if two calendar objects are on the same day ignoring time.
 *
 * 28 Mar 2002 13:45 and 28 Mar 2002 06:01 would return true.
 * 28 Mar 2002 13:45 and 12 Mar 2002 13:45 would return false.
 * /*w ww .  ja  v  a 2 s.  com*/
 * 
 * @param cal1  the first calendar, not altered, not null
 * @param cal2  the second calendar, not altered, not null
 * @return true if they represent the same day
 * @throws IllegalArgumentException if either calendar is <code>null</code>
 * @since 2.1
 */
public static boolean isSameDay(Calendar cal1, Calendar cal2) {
    if (cal1 == null || cal2 == null) {
        throw new IllegalArgumentException("The date must not be null");
    }
    return (cal1.get(Calendar.ERA) == cal2.get(Calendar.ERA)
            && cal1.get(Calendar.YEAR) == cal2.get(Calendar.YEAR)
            && cal1.get(Calendar.DAY_OF_YEAR) == cal2.get(Calendar.DAY_OF_YEAR));
}

From source file:org.apache.hadoop.hive.ql.udf.UDFADD_MONTHS.java

public Text evaluate(Text dateString1, IntWritable months) {

    if (dateString1 == null || months == null) {
        return null;
    }//from  w w w. j  a v a  2  s  . com

    try {

        Date inDate;
        boolean hasLine = false;
        for (int i = 0; i < dateString1.toString().length(); i++) {
            if (dateString1.toString().charAt(i) == '-') {
                hasLine = true;
                break;
            }
        }

        if (!hasLine) {
            if (dateString1.toString().trim().length() > 8) {
                return null;
            }
            inDate = formatterNoLine.parse(dateString1.toString().trim());
        } else {
            inDate = formatter.parse(dateString1.toString());
        }

        calendar.setTime(inDate);
        int fff = calendar.get(Calendar.ERA);
        if (fff == GregorianCalendar.BC)
            return null;
        int yyy = calendar.get(Calendar.YEAR);
        if ((yyy < 1900) || (yyy > 9999))
            return null;
        int mmm = calendar.get(Calendar.MONTH);
        int rrr = yyy + (months.get() + mmm) / 12;
        if ((rrr < 1900) || (rrr > 9999))
            return null;
        calendar.add(Calendar.DAY_OF_MONTH, 1);
        if (calendar.get(Calendar.DAY_OF_MONTH) == 1) {
            calendar.add(Calendar.MONTH, months.get());
            calendar.add(Calendar.DAY_OF_MONTH, -1);
        } else {
            calendar.add(Calendar.DAY_OF_MONTH, -1);
            calendar.add(Calendar.MONTH, months.get());
        }
        yyy = calendar.get(Calendar.YEAR);
        if ((yyy < 1900) || (yyy > 9999))
            return null;
        Date newDate = calendar.getTime();

        if (!hasLine) {
            result.set(formatterNoLine.format(newDate));
        } else {
            result.set(formatter.format(newDate));
        }
        return result;
    } catch (ParseException e) {
        return null;
    }
}

From source file:DateUtils.java

/**
 * <p>Checks if the first calendar date is before the second calendar date ignoring time.</p>
 * @param cal1 the first calendar, not altered, not null.
 * @param cal2 the second calendar, not altered, not null.
 * @return true if cal1 date is before cal2 date ignoring time.
 * @throws IllegalArgumentException if either of the calendars are <code>null</code>
 *///from   ww  w  .  jav  a 2s  . com
public static boolean isBeforeDay(Calendar cal1, Calendar cal2) {
    if (cal1 == null || cal2 == null) {
        throw new IllegalArgumentException("The dates must not be null");
    }
    if (cal1.get(Calendar.ERA) < cal2.get(Calendar.ERA))
        return true;
    if (cal1.get(Calendar.ERA) > cal2.get(Calendar.ERA))
        return false;
    if (cal1.get(Calendar.YEAR) < cal2.get(Calendar.YEAR))
        return true;
    if (cal1.get(Calendar.YEAR) > cal2.get(Calendar.YEAR))
        return false;
    return cal1.get(Calendar.DAY_OF_YEAR) < cal2.get(Calendar.DAY_OF_YEAR);
}

From source file:com.brightcove.com.zartan.verifier.video.PlaysReportingVerifier.java

public static boolean isToday(Long uploadDate) {
    Calendar uploaded = Calendar.getInstance();
    uploaded.setTime(new Date(uploadDate));
    Calendar now = Calendar.getInstance();
    now.setTime(new Date());

    return (uploaded.get(Calendar.ERA) == now.get(Calendar.ERA)
            && uploaded.get(Calendar.YEAR) == now.get(Calendar.YEAR)
            && uploaded.get(Calendar.DAY_OF_YEAR) == now.get(Calendar.DAY_OF_YEAR));
}

From source file:org.apache.hadoop.hive.ql.udf.UDFMONTHS_BETWEEN.java

public DoubleWritable evaluate(Text date1, Text date2) {

    if (date1 == null || date2 == null) {
        return null;
    }/*from   w  w  w .  ja v a  2 s  .  co  m*/

    try {

        Date date_1, date_2;
        boolean hasLine = false;

        for (int i = 0; i < date1.toString().length(); i++) {
            if (date1.toString().charAt(i) == '-') {
                hasLine = true;
                break;
            }
        }
        if (!hasLine) {
            if (date1.toString().trim().length() > 8) {
                return null;
            }
            date_1 = formatterNoLine.parse(date1.toString().trim());
        } else {
            date_1 = formatter.parse(date1.toString());
        }

        hasLine = false;
        for (int i = 0; i < date2.toString().length(); i++) {
            if (date2.toString().charAt(i) == '-') {
                hasLine = true;
                break;
            }
        }
        if (!hasLine) {
            if (date2.toString().trim().length() > 8) {
                return null;
            }
            date_2 = formatterNoLine.parse(date2.toString().trim());
        } else {
            date_2 = formatter.parse(date2.toString());
        }

        calendar.setTime(date_1);
        if (calendar.get(Calendar.ERA) == GregorianCalendar.BC)
            return null;
        int yyy = calendar.get(Calendar.YEAR);
        if ((yyy < 1900) || (yyy > 9999))
            return null;
        int d_1 = calendar.get(Calendar.DAY_OF_MONTH);
        int m_1 = calendar.get(Calendar.MONTH);
        int y_1 = calendar.get(Calendar.YEAR);
        calendar.setTime(date_2);
        if (calendar.get(Calendar.ERA) == GregorianCalendar.BC)
            return null;
        yyy = calendar.get(Calendar.YEAR);
        if ((yyy < 1900) || (yyy > 9999))
            return null;
        int d_2 = calendar.get(Calendar.DAY_OF_MONTH);
        int m_2 = calendar.get(Calendar.MONTH);
        int y_2 = calendar.get(Calendar.YEAR);
        double tmpR = (double) ((y_1 - y_2) * 12) + (double) (m_1 - m_2) + (double) (d_1 - d_2) / 31.0;
        result.set(tmpR);
        return result;
    } catch (ParseException e) {
        return null;
    }
}

From source file:VASSAL.chat.HttpMessageServer.java

public Message[] getMessages() {
    final ArrayList<Message> msgList = new ArrayList<Message>();
    try {/* w ww.  ja  va 2  s  .  c  om*/
        for (String msg : getMessagesURL.doGet(prepareInfo())) {
            try {
                StringTokenizer st = new StringTokenizer(msg, "&"); //$NON-NLS-1$
                String s = st.nextToken();
                String sender = s.substring(s.indexOf('=') + 1); //$NON-NLS-1$
                String date = st.nextToken();
                date = date.substring(date.indexOf('=') + 1); //$NON-NLS-1$
                s = st.nextToken(""); //$NON-NLS-1$

                String content = StringUtils
                        .join(new SequenceEncoder.Decoder(s.substring(s.indexOf('=') + 1), '|'), '\n');

                content = restorePercent(content);
                Date created = null;
                try {
                    long time = Long.parseLong(date);
                    TimeZone t = TimeZone.getDefault();
                    time += t.getOffset(Calendar.ERA, Calendar.YEAR, Calendar.MONTH, Calendar.DAY_OF_YEAR,
                            Calendar.DAY_OF_WEEK, Calendar.MILLISECOND);
                    created = new Date(time);
                } catch (NumberFormatException e1) {
                    created = new Date();
                }
                msgList.add(new Message(sender, content, created));
            } catch (NoSuchElementException ex) {
                System.err.println("Badly formatted message in HttpMessageServer:  " + msg); //$NON-NLS-1$
            }
        }
    } catch (IOException ex) {
        System.err.println("IOException retrieving messages from " + getMessagesURL); //$NON-NLS-1$
    }
    return msgList.toArray(new Message[msgList.size()]);
}

From source file:com.liferay.ide.server.remote.AbstractRemoteServerPublisher.java

protected void addToZip(IPath path, IResource resource, ZipOutputStream zip, boolean adjustGMTOffset)
        throws IOException, CoreException {
    switch (resource.getType()) {
    case IResource.FILE:
        ZipEntry zipEntry = new ZipEntry(path.toString());

        zip.putNextEntry(zipEntry);/*w  w w  .  j  ava 2 s.  co m*/

        InputStream contents = ((IFile) resource).getContents();

        if (adjustGMTOffset) {
            TimeZone currentTimeZone = TimeZone.getDefault();
            Calendar currentDt = new GregorianCalendar(currentTimeZone, Locale.getDefault());

            // Get the Offset from GMT taking current TZ into account
            int gmtOffset = currentTimeZone.getOffset(currentDt.get(Calendar.ERA), currentDt.get(Calendar.YEAR),
                    currentDt.get(Calendar.MONTH), currentDt.get(Calendar.DAY_OF_MONTH),
                    currentDt.get(Calendar.DAY_OF_WEEK), currentDt.get(Calendar.MILLISECOND));

            zipEntry.setTime(System.currentTimeMillis() + (gmtOffset * -1));
        }

        try {
            IOUtils.copy(contents, zip);
        } finally {
            contents.close();
        }

        break;

    case IResource.FOLDER:
    case IResource.PROJECT:
        IContainer container = (IContainer) resource;

        IResource[] members = container.members();

        for (IResource res : members) {
            addToZip(path.append(res.getName()), res, zip, adjustGMTOffset);
        }
    }
}

From source file:DateUtils.java

/**
 * <p>Checks if the first calendar date is after the second calendar date ignoring time.</p>
 * @param cal1 the first calendar, not altered, not null.
 * @param cal2 the second calendar, not altered, not null.
 * @return true if cal1 date is after cal2 date ignoring time.
 * @throws IllegalArgumentException if either of the calendars are <code>null</code>
 *//*from w w w . j a v  a 2  s.  c o m*/
public static boolean isAfterDay(Calendar cal1, Calendar cal2) {
    if (cal1 == null || cal2 == null) {
        throw new IllegalArgumentException("The dates must not be null");
    }
    if (cal1.get(Calendar.ERA) < cal2.get(Calendar.ERA))
        return false;
    if (cal1.get(Calendar.ERA) > cal2.get(Calendar.ERA))
        return true;
    if (cal1.get(Calendar.YEAR) < cal2.get(Calendar.YEAR))
        return false;
    if (cal1.get(Calendar.YEAR) > cal2.get(Calendar.YEAR))
        return true;
    return cal1.get(Calendar.DAY_OF_YEAR) > cal2.get(Calendar.DAY_OF_YEAR);
}

From source file:org.eclipse.wb.internal.swing.model.property.editor.models.spinner.SpinnerModelPropertyEditor.java

/**
 * @return the the name of step from {@link Calendar} fields.
 *//*from w w  w . j  a  v  a 2s.c o  m*/
private static String getDateStep(int calendarField) {
    switch (calendarField) {
    case Calendar.ERA:
        return "ERA";
    case Calendar.YEAR:
        return "YEAR";
    case Calendar.MONTH:
        return "MONTH";
    case Calendar.WEEK_OF_YEAR:
        return "WEEK_OF_YEAR";
    case Calendar.WEEK_OF_MONTH:
        return "WEEK_OF_MONTH";
    case Calendar.DAY_OF_MONTH:
        return "DAY_OF_MONTH";
    case Calendar.DAY_OF_YEAR:
        return "DAY_OF_YEAR";
    case Calendar.DAY_OF_WEEK:
        return "DAY_OF_WEEK";
    case Calendar.DAY_OF_WEEK_IN_MONTH:
        return "DAY_OF_WEEK_IN_MONTH";
    case Calendar.AM_PM:
        return "AM_PM";
    case Calendar.HOUR:
        return "HOUR";
    case Calendar.HOUR_OF_DAY:
        return "HOUR_OF_DAY";
    case Calendar.MINUTE:
        return "MINUTE";
    case Calendar.SECOND:
        return "SECOND";
    case Calendar.MILLISECOND:
        return "MILLISECOND";
    default:
        return null;
    }
}

From source file:org.callimachusproject.sql.SqlTupleResult.java

private Value value(int col) throws SQLException {
    int type = md.getColumnType(col);
    String str = rs.getString(col);
    if (str == null)
        return null;
    switch (type) {
    case java.sql.Types.NULL:
        return null;
    case java.sql.Types.DATALINK:
        return vf.createURI(str);
    case java.sql.Types.BINARY:
    case java.sql.Types.VARBINARY:
    case java.sql.Types.BIT:
    case java.sql.Types.BLOB:
    case java.sql.Types.LONGVARBINARY:
    case java.sql.Types.JAVA_OBJECT:
        return vf.createLiteral(Hex.encodeHexString(rs.getBytes(col)), XMLSchema.HEXBINARY);
    case java.sql.Types.DECIMAL:
    case java.sql.Types.NUMERIC:
        return vf.createLiteral(str, XMLSchema.DECIMAL);
    case java.sql.Types.TINYINT:
    case java.sql.Types.SMALLINT:
    case java.sql.Types.INTEGER:
    case java.sql.Types.BIGINT:
        return vf.createLiteral(str, XMLSchema.INTEGER);
    case java.sql.Types.DOUBLE:
    case java.sql.Types.FLOAT:
    case java.sql.Types.REAL:
        return vf.createLiteral(str, XMLSchema.DOUBLE);
    case java.sql.Types.BOOLEAN:
        return vf.createLiteral(rs.getBoolean(col));
    case java.sql.Types.DATE:
        GregorianCalendar date = new GregorianCalendar();
        date.setTime(rs.getDate(col));//from   w  w w  .  ja v  a 2  s  . c  o m
        date.clear(Calendar.AM_PM);
        date.clear(Calendar.HOUR);
        date.clear(Calendar.HOUR_OF_DAY);
        date.clear(Calendar.MINUTE);
        date.clear(Calendar.SECOND);
        date.clear(Calendar.MILLISECOND);
        return vf.createLiteral(df.newXMLGregorianCalendar(date));
    case java.sql.Types.TIME:
        GregorianCalendar time = new GregorianCalendar();
        time.setTime(rs.getTime(col));
        time.clear(Calendar.ERA);
        time.clear(Calendar.YEAR);
        time.clear(Calendar.MONTH);
        time.clear(Calendar.WEEK_OF_YEAR);
        time.clear(Calendar.WEEK_OF_MONTH);
        time.clear(Calendar.DATE);
        time.clear(Calendar.DAY_OF_MONTH);
        time.clear(Calendar.DAY_OF_YEAR);
        time.clear(Calendar.DAY_OF_WEEK);
        time.clear(Calendar.DAY_OF_WEEK_IN_MONTH);
        return vf.createLiteral(df.newXMLGregorianCalendar(time));
    case java.sql.Types.TIMESTAMP:
        return vf.createLiteral(rs.getTimestamp(col));
    case java.sql.Types.SQLXML:
        return vf.createLiteral(str, RDF.XMLLITERAL);
    case java.sql.Types.ARRAY:
    case java.sql.Types.CHAR:
    case java.sql.Types.CLOB:
    case java.sql.Types.DISTINCT:
    case java.sql.Types.LONGNVARCHAR:
    case java.sql.Types.NCHAR:
    case java.sql.Types.NCLOB:
    case java.sql.Types.NVARCHAR:
    case java.sql.Types.OTHER:
    case java.sql.Types.REF:
    case java.sql.Types.ROWID:
    case java.sql.Types.STRUCT:
    case java.sql.Types.VARCHAR:
    default:
        return vf.createLiteral(str);
    }
}