Example usage for org.joda.time DateTime toGregorianCalendar

List of usage examples for org.joda.time DateTime toGregorianCalendar

Introduction

In this page you can find the example usage for org.joda.time DateTime toGregorianCalendar.

Prototype

public GregorianCalendar toGregorianCalendar() 

Source Link

Document

Get the date time as a java.util.GregorianCalendar, assigning exactly the same millisecond instant.

Usage

From source file:org.joda.example.time.Examples.java

License:Apache License

private void runDateTime() {
    System.out.println("DateTime");
    System.out.println("=======");
    System.out.println(/*from  ww w  . j  a  v a2  s. c om*/
            "DateTime stores a the date and time using millisecs from 1970-01-01T00:00:00Z internally");
    System.out.println("DateTime is immutable and thread-safe");
    System.out.println("                      in = new DateTime()");
    DateTime in = new DateTime();
    System.out.println("Millisecond time:     in.getMillis():           " + in.getMillis());
    System.out.println("ISO string version:   in.toString():            " + in.toString());
    System.out.println("ISO chronology:       in.getChronology():       " + in.getChronology());
    System.out.println("Your time zone:       in.getDateTimeZone():     " + in.getZone());
    System.out.println("Change millis:        in.withMillis(0):         " + in.withMillis(0L));
    System.out.println("");
    System.out.println("Get year:             in.getYear():             " + in.getYear());
    System.out.println("Get monthOfYear:      in.getMonthOfYear():      " + in.getMonthOfYear());
    System.out.println("Get dayOfMonth:       in.getDayOfMonth():       " + in.getDayOfMonth());
    System.out.println("...");
    System.out.println("Property access:      in.dayOfWeek().get():                   " + in.dayOfWeek().get());
    System.out.println(
            "Day of week as text:  in.dayOfWeek().getAsText():             " + in.dayOfWeek().getAsText());
    System.out.println(
            "Day as short text:    in.dayOfWeek().getAsShortText():        " + in.dayOfWeek().getAsShortText());
    System.out.println("Day in french:        in.dayOfWeek().getAsText(Locale.FRENCH):"
            + in.dayOfWeek().getAsText(Locale.FRENCH));
    System.out.println("Max allowed value:    in.dayOfWeek().getMaximumValue():       "
            + in.dayOfWeek().getMaximumValue());
    System.out.println("Min allowed value:    in.dayOfWeek().getMinimumValue():       "
            + in.dayOfWeek().getMinimumValue());
    System.out.println(
            "Copy & set to Jan:    in.monthOfYear().setCopy(1):            " + in.monthOfYear().setCopy(1));
    System.out.println(
            "Copy & add 14 months: in.monthOfYear().addCopy(14):           " + in.monthOfYear().addToCopy(14));
    System.out.println("Add 14 mnths in field:in.monthOfYear().addWrapFieldCopy(14):  "
            + in.monthOfYear().addWrapFieldToCopy(14));
    System.out.println("...");
    System.out.println("Convert to Instant:   in.toInstant():           " + in.toInstant());
    System.out.println("Convert to DateTime:  in.toDateTime():          " + in.toDateTime());
    System.out.println("Convert to MutableDT: in.toMutableDateTime():   " + in.toMutableDateTime());
    System.out.println("Convert to Date:      in.toDate():              " + in.toDate());
    System.out.println("Convert to Calendar:  in.toCalendar(Locale.UK): "
            + in.toCalendar(Locale.UK).toString().substring(0, 46));
    System.out.println("Convert to GregCal:   in.toGregorianCalendar(): "
            + in.toGregorianCalendar().toString().substring(0, 46));
    System.out.println("");
    System.out.println("                      in2 = new DateTime(in.getMillis() + 10)");
    DateTime in2 = new DateTime(in.getMillis() + 10);
    System.out.println("Equals ms and chrono: in.equals(in2):           " + in.equals(in2));
    System.out.println("Compare millisecond:  in.compareTo(in2):        " + in.compareTo(in2));
    System.out.println("Compare millisecond:  in.isEqual(in2):          " + in.isEqual(in2));
    System.out.println("Compare millisecond:  in.isAfter(in2):          " + in.isAfter(in2));
    System.out.println("Compare millisecond:  in.isBefore(in2):         " + in.isBefore(in2));
}

From source file:org.jruby.truffle.core.time.RubyDateFormatter.java

License:LGPL

private int formatWeekYear(DateTime dt, int firstDayOfWeek) {
    Calendar dtCalendar = dt.toGregorianCalendar();
    dtCalendar.setFirstDayOfWeek(firstDayOfWeek);
    dtCalendar.setMinimalDaysInFirstWeek(7);
    int value = dtCalendar.get(Calendar.WEEK_OF_YEAR);
    if ((value == 52 || value == 53) && (dtCalendar.get(Calendar.MONTH) == Calendar.JANUARY)) {
        // MRI behavior: Week values are monotonous.
        // So, weeks that effectively belong to previous year,
        // will get the value of 0, not 52 or 53, as in Java.
        value = 0;//ww  w  .  ja va2 s  .c  o  m
    }
    return value;
}

From source file:org.jruby.util.RubyDateFormatter.java

License:LGPL

private int formatWeekYear(DateTime dt, int firstDayOfWeek) {
    java.util.Calendar dtCalendar = dt.toGregorianCalendar();
    dtCalendar.setFirstDayOfWeek(firstDayOfWeek);
    dtCalendar.setMinimalDaysInFirstWeek(7);
    int value = dtCalendar.get(java.util.Calendar.WEEK_OF_YEAR);
    if ((value == 52 || value == 53) && (dtCalendar.get(Calendar.MONTH) == Calendar.JANUARY)) {
        // MRI behavior: Week values are monotonous.
        // So, weeks that effectively belong to previous year,
        // will get the value of 0, not 52 or 53, as in Java.
        value = 0;//from w ww . ja  v a  2 s .c o  m
    }
    return value;
}

From source file:org.mongoste.core.StatsEngineTestUtils.java

License:Open Source License

public static void buildSamples(StatsEngine statsEngine) throws StatsEngineException {
    StatEvent event;//from  w ww.  j a v a  2 s  . c  o  m
    DateTime dt = DateUtil.getDateTimeUTC();
    dt = DateUtil.trimTime(dt);
    Calendar cal = dt.toGregorianCalendar();
    cal.set(Calendar.DATE, 1);
    //cal.set(Calendar.MONTH,C);
    cal.set(Calendar.HOUR_OF_DAY, 0);
    int metaCount = 0;
    for (int j = 0; j < 4; j++) {
        for (int i = 0; i < 10; i++) {
            event = new StatEvent();
            event.setClientId("client1");
            //cal.set(Calendar.HOUR,i);
            //cal.set(Calendar.HOUR,i % 11);
            event.setDate(cal.getTime());
            //event.setDate(DateUtil.getDateGMT0());
            event.setTarget("target" + (1 + j % 2));
            event.setAction("view");
            event.setTargetType("work");
            //event.getMetadata().put("sessionId", j * 100 + metaCount );
            event.getMetadata().put("ip", "192.168.1." + ((metaCount) % 10));
            statsEngine.handleEvent(event);
            metaCount = metaCount + (i % 2);
            cal.add(Calendar.HOUR_OF_DAY, i % 4);
        }
        cal.add(Calendar.DATE, 1);
    }
}

From source file:org.mousephenotype.dcc.exportlibrary.datastructure.converters.DatatypeConverter.java

License:Apache License

public static Calendar parseDateTime(String lexicalDate) {
    if (lexicalDate == null || lexicalDate.equals("")) {
        logger.trace("parsing is null or empty");
        return null;
    }/*from  w w w. j  a v  a  2s. c om*/
    logger.trace("parsing dateTime {}", lexicalDate);
    org.joda.time.DateTime dateTime = org.joda.time.DateTime.parse(lexicalDate,
            DateTimeFormat.forPattern(dateTimeXMLpattern).withZoneUTC());
    return dateTime.toGregorianCalendar();
}

From source file:org.myorganisation.myproject.quickstart.backend.model.Person.java

License:Apache License

/**
 * Compound constructor creating a Person wrapping the supplied data.
 *
 * @param firstName The first name of this Person.
 * @param lastName  The last name of this Person.
 * @param birthday  The birthday of this Person.
 *//*w w w .ja v  a 2 s . c  om*/
public Person(final String firstName, final String lastName, final DateTime birthday) {

    this.firstName = firstName;
    this.lastName = lastName;
    this.birthday = birthday.toGregorianCalendar();
}

From source file:org.n52.oxf.valueDomains.time.TimePosition.java

License:Open Source License

@Override
public Calendar getCalendar() {
    DateTime dateTime = DateTime.parse(this.toISO8601Format());
    return dateTime.toGregorianCalendar();
}

From source file:org.onosproject.drivers.bti.Bti7000SnmpAlarmConsumer.java

License:Apache License

/**
 * This method is similar to SNMP4J approach with some fixes for the 11-bytes version (ie the one with timezone
 * offset).//from  www. j a  v a  2  s .  co  m
 * <p>
 * For original makeCalendar refer @see http://www.snmp4j.org/agent/doc/org/snmp4j/agent/mo/snmp/DateAndTime.html
 * <p>
 * Creates a <code>GregorianCalendar</code> from a properly formatted SNMP4J DateAndTime <code>OctetString</code>.
 *
 * @param dateAndTimeValue an OctetString conforming to the DateAndTime TC.
 * @return the corresponding <code>GregorianCalendar</code> instance.
 */
public static GregorianCalendar btiMakeCalendar(OctetString dateAndTimeValue) {
    int year = (dateAndTimeValue.get(0) & 0xFF) * 256 + (dateAndTimeValue.get(1) & 0xFF);
    int month = (dateAndTimeValue.get(2) & 0xFF);
    int date = (dateAndTimeValue.get(3) & 0xFF);
    int hour = (dateAndTimeValue.get(4) & 0xFF);
    int minute = (dateAndTimeValue.get(5) & 0xFF);
    int second = (dateAndTimeValue.get(6) & 0xFF);
    int deci = (dateAndTimeValue.get(7) & 0xFF);
    GregorianCalendar gc = new GregorianCalendar(year, month - 1, date, hour, minute, second);
    gc.set(Calendar.MILLISECOND, deci * 100);

    if (dateAndTimeValue.length() == 11) {
        char directionOfOffset = (char) dateAndTimeValue.get(8);
        int hoursOffset = directionOfOffset == '+' ? dateAndTimeValue.get(9) : -dateAndTimeValue.get(9);
        org.joda.time.DateTimeZone offset = org.joda.time.DateTimeZone.forOffsetHoursMinutes(hoursOffset,
                dateAndTimeValue.get(10));
        org.joda.time.DateTime dt = new org.joda.time.DateTime(year, month, date, hour, minute, second, offset);
        return dt.toGregorianCalendar();
    }
    return gc;
}

From source file:org.openehealth.ipf.commons.ihe.xds.core.metadata.jaxbadapters.DateTimeAdapter.java

License:Apache License

@Override
public Calendar marshal(DateTime dateTime) throws Exception {
    if (dateTime == null) {
        return null;
    }//  ww w. j  a  v  a2s  .  c  o  m
    return dateTime.toGregorianCalendar();
}

From source file:org.semantictools.jsonld.LdLiteral.java

License:Apache License

/**
 * Returns a Calendar representation of this literal.
 * The underlying value must be a string in ISO 8601 format.
 *//*from ww  w. j a va 2 s.c o  m*/
public Calendar getCalendarValue() {

    Calendar result = null;
    String text = getStringValue();
    try {
        DateTime datetime = new DateTime(text);
        result = datetime.toGregorianCalendar();
    } catch (Exception oops) {
        throw new RuntimeException(oops);
    }
    return result;
}