Java Code Examples for org.joda.time.DateTime#withZone()

The following examples show how to use org.joda.time.DateTime#withZone() . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
/**
 * Method to return a "normalized" version of the input Date
 * whose time is reset to the absolute start of that same day
 * (first millisecond of first second of first minute of first hour).
 *    
 * @param dateInst - instance of Date
 * @return - instance of Date as described
 * @throws AdeException 
 */
public static Date startOfDayUsingOutputTimeZone(Date dateInst) throws AdeException {

    if (outputTimeZone == null) {
        final TimeZone outputTimezone = Ade.getAde().getConfigProperties().getOutputTimeZone();
        outputTimeZone = DateTimeZone.forOffsetMillis(outputTimezone.getRawOffset());
    }

    if (dateInst == null) {
        throw new IllegalArgumentException();
    }

    /* Set start of today */
    DateTime startOFDay = new DateTime(dateInst);
    startOFDay = startOFDay.withZone(outputTimeZone);
    startOFDay = startOFDay.withTimeAtStartOfDay();

    return startOFDay.toDate();

}
 
Example 2
public void testFormat_dateTime() {
        DateTime dt = new DateTime(2004, 6, 9, 10, 20, 30, 40, UTC);
        assertEquals("2004-06-09T10:20:30.040Z", ISODateTimeFormat.dateTime().print(dt));
        
        dt = dt.withZone(LONDON);
        assertEquals("2004-06-09T11:20:30.040+01:00", ISODateTimeFormat.dateTime().print(dt));
        
        dt = dt.withZone(PARIS);
        assertEquals("2004-06-09T12:20:30.040+02:00", ISODateTimeFormat.dateTime().print(dt));
        
//        dt = dt.withZone(LONDON);
//        assertEquals("2004-06-09T11:20:30.040+01:00", ISODateTimeFormat.getInstance(PARIS).dateTime().print(dt));
//        
//        dt = dt.withZone(LONDON);
//        assertEquals("2004-06-09T12:20:30.040+02:00", ISODateTimeFormat.dateTime().print(dt.getMillis(), PARIS));
//        
//        dt = new DateTime(2004, 6, 9, 10, 20, 30, 40, CopticChronology.getInstance());
//        assertEquals("2288-02-19T10:20:30.040Z", ISODateTimeFormat.dateTime().print(dt));
//        
//        dt = new DateTime(2004, 6, 9, 10, 20, 30, 40, CopticChronology.getInstance());
//        assertEquals("2004-06-09T10:20:30.040Z", ISODateTimeFormat.getInstance(CopticChronology.getInstance()).dateTime().print(dt));
    }
 
Example 3
public void testFormat_dateTime() {
        DateTime dt = new DateTime(2004, 6, 9, 10, 20, 30, 40, UTC);
        assertEquals("2004-06-09T10:20:30.040Z", ISODateTimeFormat.dateTime().print(dt));
        
        dt = dt.withZone(LONDON);
        assertEquals("2004-06-09T11:20:30.040+01:00", ISODateTimeFormat.dateTime().print(dt));
        
        dt = dt.withZone(PARIS);
        assertEquals("2004-06-09T12:20:30.040+02:00", ISODateTimeFormat.dateTime().print(dt));
        
//        dt = dt.withZone(LONDON);
//        assertEquals("2004-06-09T11:20:30.040+01:00", ISODateTimeFormat.getInstance(PARIS).dateTime().print(dt));
//        
//        dt = dt.withZone(LONDON);
//        assertEquals("2004-06-09T12:20:30.040+02:00", ISODateTimeFormat.dateTime().print(dt.getMillis(), PARIS));
//        
//        dt = new DateTime(2004, 6, 9, 10, 20, 30, 40, CopticChronology.getInstance());
//        assertEquals("2288-02-19T10:20:30.040Z", ISODateTimeFormat.dateTime().print(dt));
//        
//        dt = new DateTime(2004, 6, 9, 10, 20, 30, 40, CopticChronology.getInstance());
//        assertEquals("2004-06-09T10:20:30.040Z", ISODateTimeFormat.getInstance(CopticChronology.getInstance()).dateTime().print(dt));
    }
 
Example 4
public void testFormat_basicDateTime() {
    DateTime dt = new DateTime(2004, 6, 9, 10, 20, 30, 40, UTC);
    assertEquals("20040609T102030.040Z", ISODateTimeFormat.basicDateTime().print(dt));
    
    dt = dt.withZone(LONDON);
    assertEquals("20040609T112030.040+0100", ISODateTimeFormat.basicDateTime().print(dt));
    
    dt = dt.withZone(PARIS);
    assertEquals("20040609T122030.040+0200", ISODateTimeFormat.basicDateTime().print(dt));
}
 
Example 5
public void testFormat_dateHourMinuteSecond() {
    DateTime dt = new DateTime(2004, 6, 9, 10, 20, 30, 40, UTC);
    assertEquals("2004-06-09T10:20:30", ISODateTimeFormat.dateHourMinuteSecond().print(dt));
    
    dt = dt.withZone(LONDON);
    assertEquals("2004-06-09T11:20:30", ISODateTimeFormat.dateHourMinuteSecond().print(dt));
    
    dt = dt.withZone(PARIS);
    assertEquals("2004-06-09T12:20:30", ISODateTimeFormat.dateHourMinuteSecond().print(dt));
}
 
Example 6
public void testFormat_basicWeekDateTime() {
    DateTime dt = new DateTime(2004, 6, 9, 10, 20, 30, 40, UTC);
    assertEquals("2004W243T102030.040Z", ISODateTimeFormat.basicWeekDateTime().print(dt));
    
    dt = dt.withZone(LONDON);
    assertEquals("2004W243T112030.040+0100", ISODateTimeFormat.basicWeekDateTime().print(dt));
    
    dt = dt.withZone(PARIS);
    assertEquals("2004W243T122030.040+0200", ISODateTimeFormat.basicWeekDateTime().print(dt));
}
 
Example 7
public void testFormat_dateHourMinuteSecond() {
    DateTime dt = new DateTime(2004, 6, 9, 10, 20, 30, 40, UTC);
    assertEquals("2004-06-09T10:20:30", ISODateTimeFormat.dateHourMinuteSecond().print(dt));
    
    dt = dt.withZone(LONDON);
    assertEquals("2004-06-09T11:20:30", ISODateTimeFormat.dateHourMinuteSecond().print(dt));
    
    dt = dt.withZone(PARIS);
    assertEquals("2004-06-09T12:20:30", ISODateTimeFormat.dateHourMinuteSecond().print(dt));
}
 
Example 8
public void testFormat_dayOfWeek() {
    DateTime dt = new DateTime(2004, 6, 9, 10, 20, 30, 40, UTC);
    DateTimeFormatter f = DateTimeFormat.forPattern("e").withLocale(Locale.UK);
    assertEquals(dt.toString(), "3", f.print(dt));
    
    dt = dt.withZone(NEWYORK);
    assertEquals(dt.toString(), "3", f.print(dt));
    
    dt = dt.withZone(TOKYO);
    assertEquals(dt.toString(), "3", f.print(dt));
}
 
Example 9
public void testFormat_hourMinuteSecondFraction() {
    DateTime dt = new DateTime(2004, 6, 9, 10, 20, 30, 40, UTC);
    assertEquals("10:20:30.040", ISODateTimeFormat.hourMinuteSecondFraction().print(dt));
    
    dt = dt.withZone(LONDON);
    assertEquals("11:20:30.040", ISODateTimeFormat.hourMinuteSecondFraction().print(dt));
    
    dt = dt.withZone(PARIS);
    assertEquals("12:20:30.040", ISODateTimeFormat.hourMinuteSecondFraction().print(dt));
}
 
Example 10
public void testFormat_weekyearWeekDay() {
    DateTime dt = new DateTime(2004, 6, 9, 10, 20, 30, 40, UTC);
    assertEquals("2004-W24-3", ISODateTimeFormat.weekyearWeekDay().print(dt));
    
    dt = dt.withZone(LONDON);
    assertEquals("2004-W24-3", ISODateTimeFormat.weekyearWeekDay().print(dt));
    
    dt = dt.withZone(PARIS);
    assertEquals("2004-W24-3", ISODateTimeFormat.weekyearWeekDay().print(dt));
}
 
Example 11
public void testFormat_zoneAmountID() {
    DateTime dt = new DateTime(2004, 6, 9, 10, 20, 30, 40, UTC);
    DateTimeFormatter f = DateTimeFormat.forPattern("ZZZ").withLocale(Locale.UK);
    assertEquals(dt.toString(), "UTC", f.print(dt));
    
    dt = dt.withZone(NEWYORK);
    assertEquals(dt.toString(), "America/New_York", f.print(dt));
    
    dt = dt.withZone(TOKYO);
    assertEquals(dt.toString(), "Asia/Tokyo", f.print(dt));
}
 
Example 12
public void testFormat_dateHourMinuteSecondFraction() {
    DateTime dt = new DateTime(2004, 6, 9, 10, 20, 30, 40, UTC);
    assertEquals("2004-06-09T10:20:30.040", ISODateTimeFormat.dateHourMinuteSecondFraction().print(dt));
    
    dt = dt.withZone(LONDON);
    assertEquals("2004-06-09T11:20:30.040", ISODateTimeFormat.dateHourMinuteSecondFraction().print(dt));
    
    dt = dt.withZone(PARIS);
    assertEquals("2004-06-09T12:20:30.040", ISODateTimeFormat.dateHourMinuteSecondFraction().print(dt));
}
 
Example 13
public void testFormat_basicDate() {
    DateTime dt = new DateTime(2004, 6, 9, 10, 20, 30, 40, UTC);
    assertEquals("20040609", ISODateTimeFormat.basicDate().print(dt));
    
    dt = dt.withZone(LONDON);
    assertEquals("20040609", ISODateTimeFormat.basicDate().print(dt));
    
    dt = dt.withZone(PARIS);
    assertEquals("20040609", ISODateTimeFormat.basicDate().print(dt));
}
 
Example 14
Source Project: coming   File: Cardumen_0073_t.java    License: MIT License 5 votes vote down vote up
/**
 * Parses a date-time from the given text, returning a new DateTime.
 * <p>
 * The parse will use the zone and chronology specified on this formatter.
 * <p>
 * If the text contains a time zone string then that will be taken into
 * account in adjusting the time of day as follows.
 * If the {@link #withOffsetParsed()} has been called, then the resulting
 * DateTime will have a fixed offset based on the parsed time zone.
 * Otherwise the resulting DateTime will have the zone of this formatter,
 * but the parsed zone may have caused the time to be adjusted.
 *
 * @param text  the text to parse, not null
 * @return the parsed date-time, never null
 * @throws UnsupportedOperationException if parsing is not supported
 * @throws IllegalArgumentException if the text to parse is invalid
 */
public DateTime parseDateTime(String text) {
    DateTimeParser parser = requireParser();
    
    Chronology chrono = selectChronology(null);
    DateTimeParserBucket bucket = new DateTimeParserBucket(0, chrono, iLocale, iPivotYear, iDefaultYear);
    int newPos = parser.parseInto(bucket, text, 0);
    if (newPos >= 0) {
        if (newPos >= text.length()) {
            long millis = bucket.computeMillis(true, text);
            if (iOffsetParsed && bucket.getOffsetInteger() != null) {
                int parsedOffset = bucket.getOffsetInteger();
                DateTimeZone parsedZone = DateTimeZone.forOffsetMillis(parsedOffset);
                chrono = chrono.withZone(parsedZone);
            } else if (bucket.getZone() != null) {
                chrono = chrono.withZone(bucket.getZone());
            }
            DateTime dt = new DateTime(millis, chrono);
            if (iZone != null) {
                dt = dt.withZone(iZone);
            }
            return dt;
        }
    } else {
        newPos = ~newPos;
    }
    throw new IllegalArgumentException(FormatUtils.createErrorMessage(text, newPos));
}
 
Example 15
public void testFormat_basicTTimeNoMillis() {
    DateTime dt = new DateTime(2004, 6, 9, 10, 20, 30, 40, UTC);
    assertEquals("T102030Z", ISODateTimeFormat.basicTTimeNoMillis().print(dt));
    
    dt = dt.withZone(LONDON);
    assertEquals("T112030+0100", ISODateTimeFormat.basicTTimeNoMillis().print(dt));
    
    dt = dt.withZone(PARIS);
    assertEquals("T122030+0200", ISODateTimeFormat.basicTTimeNoMillis().print(dt));
}
 
Example 16
public void testFormat_fractionOfSecond() {
    DateTime dt = new DateTime(2004, 6, 9, 10, 20, 30, 40, UTC);
    DateTimeFormatter f = DateTimeFormat.forPattern("SSS").withLocale(Locale.UK);
    assertEquals(dt.toString(), "040", f.print(dt));
    
    dt = dt.withZone(NEWYORK);
    assertEquals(dt.toString(), "040", f.print(dt));
    
    dt = dt.withZone(TOKYO);
    assertEquals(dt.toString(), "040", f.print(dt));
}
 
Example 17
public void testFormat_dayOfWeekText() {
    DateTime dt = new DateTime(2004, 6, 9, 10, 20, 30, 40, UTC);
    DateTimeFormatter f = DateTimeFormat.forPattern("EEEE").withLocale(Locale.UK);
    assertEquals(dt.toString(), "Wednesday", f.print(dt));
    
    dt = dt.withZone(NEWYORK);
    assertEquals(dt.toString(), "Wednesday", f.print(dt));
    
    dt = dt.withZone(TOKYO);
    assertEquals(dt.toString(), "Wednesday", f.print(dt));
    
    f = f.withLocale(Locale.FRENCH);
    assertEquals(dt.toString(), "mercredi", f.print(dt));
}
 
Example 18
public void testFormat_zoneText() {
    DateTime dt = new DateTime(2004, 6, 9, 10, 20, 30, 40, UTC);
    DateTimeFormatter f = DateTimeFormat.forPattern("z").withLocale(Locale.UK);
    assertEquals(dt.toString(), "UTC", f.print(dt));
    
    dt = dt.withZone(NEWYORK);
    assertEquals(dt.toString(), "EDT", f.print(dt));
    
    dt = dt.withZone(TOKYO);
    assertEquals(dt.toString(), "JST", f.print(dt));
}
 
Example 19
public void testFormat_ordinalDateTime() {
    DateTime dt = new DateTime(2004, 6, 9, 10, 20, 30, 40, UTC);
    assertEquals("2004-161T10:20:30.040Z", ISODateTimeFormat.ordinalDateTime().print(dt));
    
    dt = dt.withZone(LONDON);
    assertEquals("2004-161T11:20:30.040+01:00", ISODateTimeFormat.ordinalDateTime().print(dt));
    
    dt = dt.withZone(PARIS);
    assertEquals("2004-161T12:20:30.040+02:00", ISODateTimeFormat.ordinalDateTime().print(dt));
}
 
Example 20
Source Project: Raigad   File: ElasticsearchUtils.java    License: Apache License 2.0 4 votes vote down vote up
/**
 * Repository Name is Today's Date in yyyyMMdd format eg. 20140630
 *
 * @return Repository Name
 */
public static String getS3RepositoryName() {
    DateTime dateTime = new DateTime();
    DateTime dateTimeGmt = dateTime.withZone(currentZone);
    return formatDate(dateTimeGmt, S3_REPO_DATE_FORMAT);
}