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

The following examples show how to use org.joda.time.DateTime#withZoneRetainFields() . 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
@Override
public String toNonNullValue(DateTime value) {

    DateTimeZone correctTimeZone = value.getZone();
    DateTime utcDateTime = value.withZone(DateTimeZone.UTC);
    DateTime utcDateTimeWithCorrectTimeZone = utcDateTime.withZoneRetainFields(correctTimeZone);
    
    int dayOfMonth = utcDateTimeWithCorrectTimeZone.getDayOfMonth();
    
    String dateTimeAsString = DATE_TIME_PRINTER_PREFIX.print(utcDateTimeWithCorrectTimeZone);
    
    if (dayOfMonth < 10) {
    	dateTimeAsString = dateTimeAsString + "0";
    }
    dateTimeAsString = dateTimeAsString + dayOfMonth;
    
    dateTimeAsString = dateTimeAsString + DATE_TIME_PRINTER_SUFFIX.print(utcDateTimeWithCorrectTimeZone);
    
    return dateTimeAsString;
}
 
Example 2
public static DateTime getDateTime() {
	DateTime dt = new DateTime();
	if (canApplyZoneChange) {
		return dt.withZoneRetainFields(DateTimeZone.forID(GSConfig.TIME_ZONE_ID));
	}
	return dt;
}
 
Example 3
public static DateTime getDateTime(String isoDateTime) {
	DateTime dt = new DateTime(isoDateTime);
	if (canApplyZoneChange) {
		return dt.withZoneRetainFields(DateTimeZone.forID(GSConfig.TIME_ZONE_ID));
	}
	return dt;
}
 
Example 4
public static DateTime getDateTime(GregorianCalendar calendar) {
	DateTime dt = new DateTime(calendar);
	if (canApplyZoneChange) {
		return dt.withZoneRetainFields(DateTimeZone.forID(GSConfig.TIME_ZONE_ID));
	}
	return dt;
}
 
Example 5
public static DateTime getDateTime(long millisSinceSeventies) {
	DateTime dt = new DateTime(millisSinceSeventies);
	if (canApplyZoneChange) {
		return dt.withZoneRetainFields(DateTimeZone.forID(GSConfig.TIME_ZONE_ID));
	}
	return dt;
}
 
Example 6
@Override
public DateTime fromNonNullValue(String s) {

    DateTime parsedDateTime = DATE_TIME_PARSER.parseDateTime(s);
    DateTimeZone correctTimeZone = parsedDateTime.getZone();
    DateTime utcDateTime = parsedDateTime.withZoneRetainFields(DateTimeZone.UTC);
    DateTime correctedDateTime = utcDateTime.withZone(correctTimeZone);
    return correctedDateTime;
}
 
Example 7
/**
 * Retrieves the date parsed from the header of a log. Unless otherwise defined in the properties file,
 * we have to use some logic to figure out the year. After parsing the date, we need to correct the time-zone. 
 * Then we set the dateTime to the current year. Now we need to check the dateTime and see if it's after today.
 * The logic is as follows:
 *      - If Log time-stamp < End of day of today 
 *          (comparing Month, Day, Hour, Minutes, Seconds, with year missing), 
 *          assume it's this year.
 *      - If Log time-stamp > End of day of today 
 *          (comparing Month, Day, Hour, Minutes, Seconds, with year missing), 
 *          assume it's previous year.
 * 
 * The following restrictions will be made to customer for BulkLoad:
 *      - Cannot upload logs older than 11 months.
 *      - Cannot upload logs that are continuous for more than 11 months.
 * 
 * Note: END OF TODAY is purposely chosen instead of START OF TODAY in case a user bulk loads logs that 
 * belongs to today.  It's not possible/likely that a user would bulk load logs from last year of the 
 * same day with the restriction we specified above.
 * @param source the source name string value.
 * @param s the date and time string value.
 * @return Date object with date/time-stamp of the Linux log.
 */
@Override
public final Date toDate(String source, String s) {
    DateTime dt = null;
    for (DateTimeFormatter fmt : dt_formatters) {
        try {
            dt = fmt.parseDateTime(s);
            dt = dt.withZoneRetainFields(INPUT_TIME_ZONE);
            dt = dt.withZone(OUTPUT_TIME_ZONE);

            /* Year must be set after all the time is normalized to the timezone */
            dt = dt.withYear(curYear);

            if (s_linuxAdeExtProperties.isYearDefined()) {
                yearSetter = LinuxSyslogYearSetter.getYearSetter(source);

                /* If years is defined, then, use the defined year as a starting year */
                final int yearToUse = yearSetter.getDesiredYear(dt);
                dt = dt.withYear(yearToUse);
            } else if (dt.isAfter(END_OF_TODAY)) {
                /* Set DateTime to previous year */
                dt = dt.withYear(curYear - 1);
            } else {
                dt = dt.withYear(curYear);
            }

            /* AdeCore will take the Java Date object, and convert 
             * it to the output time-zone, then extract the hour. */
            return dt.toDate();
        } catch (IllegalArgumentException e) {
            /* This exception can occur normally when iterating
             * through the DateTimeFormatter objects. It is only 
             * an error worth noting when the dt object is not null.
             */
            if (dt != null) {
                s_logger.error("Invalid argument encountered.", e);
            }
        }
    }
    throw new IllegalArgumentException("Failed to parse date " + s);
}
 
Example 8
Source Project: beam   File: DateFunctions.java    License: Apache License 2.0 4 votes vote down vote up
public DateTime date(DateTime ts, String timezone) {
  return ts.withZoneRetainFields(DateTimeZone.forTimeZone(TimeZone.getTimeZone(timezone)));
}
 
Example 9
Source Project: sdk-rest   File: RestUtil.java    License: MIT License 4 votes vote down vote up
public static DateTime nowInESTWithUTCTimeZone() {
	DateTime now = new DateTime(timeZone);
	return now.withZoneRetainFields(DateTimeZone.UTC);
}
 
Example 10
Source Project: joda-time-android   File: DateUtils.java    License: Apache License 2.0 4 votes vote down vote up
private static long toMillis(ReadableInstant time) {
    DateTime dateTime = time instanceof DateTime ? (DateTime) time : new DateTime(time);
    DateTime utcDateTime = dateTime.withZoneRetainFields(DateTimeZone.UTC);
    return utcDateTime.getMillis();
}