Example usage for org.joda.time DateTime withZoneRetainFields

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

Introduction

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

Prototype

public DateTime withZoneRetainFields(DateTimeZone newZone) 

Source Link

Document

Returns a copy of this datetime with a different time zone, preserving the field values.

Usage

From source file:com.aionemu.gameserver.utils.gametime.DateTimeUtil.java

License:Open Source License

public static DateTime getDateTime() {
    DateTime dt = new DateTime();
    if (canApplyZoneChange) {
        return dt.withZoneRetainFields(DateTimeZone.forID(GSConfig.TIME_ZONE_ID));
    }//from  w  w w . ja  v a 2 s. com
    return dt;
}

From source file:com.aionemu.gameserver.utils.gametime.DateTimeUtil.java

License:Open Source License

public static DateTime getDateTime(String isoDateTime) {
    DateTime dt = new DateTime(isoDateTime);
    if (canApplyZoneChange) {
        return dt.withZoneRetainFields(DateTimeZone.forID(GSConfig.TIME_ZONE_ID));
    }//  w w  w  .  j av  a 2 s . c o m
    return dt;
}

From source file:com.aionemu.gameserver.utils.gametime.DateTimeUtil.java

License:Open Source License

public static DateTime getDateTime(GregorianCalendar calendar) {
    DateTime dt = new DateTime(calendar);
    if (canApplyZoneChange) {
        return dt.withZoneRetainFields(DateTimeZone.forID(GSConfig.TIME_ZONE_ID));
    }//  w ww .  j  a  v  a 2  s . c  o  m
    return dt;
}

From source file:com.aionemu.gameserver.utils.gametime.DateTimeUtil.java

License:Open Source License

public static DateTime getDateTime(long millisSinceSeventies) {
    DateTime dt = new DateTime(millisSinceSeventies);
    if (canApplyZoneChange) {
        return dt.withZoneRetainFields(DateTimeZone.forID(GSConfig.TIME_ZONE_ID));
    }/*  ww w  .  ja  v a 2s  .c  o m*/
    return dt;
}

From source file:com.claresco.tinman.sql.XapiStatementSQLReader.java

License:Open Source License

/**
 * /* ww  w .ja va  2s . c o  m*/
 * Definition:
 *   Helper method that will create a hashmap from result
 *
 * Params:
 *
 *
 */
private HashMap<Integer, XapiStatement> getStatementFromResult() throws SQLException {
    HashMap<Integer, XapiStatement> statementArray = new HashMap<Integer, XapiStatement>();

    while (myResult.next()) {
        UUID theId = UUID.fromString(myResult.getString("statementuuid"));
        XapiActor theActor = myActorReader.retrieveByID(myResult.getInt("actorid"));
        XapiVerb theVerb = myVerbReader.retrieveByID(myResult.getInt("verbid"));
        XapiObject theObject = myObjectReader.retrieveByID(myResult.getInt("objectid"));
        XapiResult theResult = null;
        int theResultID = myResult.getInt("resultid");
        if (!myResult.wasNull()) {
            theResult = myResultReader.retrieveByID(theResultID);
        }

        XapiContext theContext = null;
        int theContextID = myResult.getInt("contextid");
        if (!myResult.wasNull()) {
            theContext = myContextReader.retrieveByID(theContextID);
        }

        String theTSString = null;

        Timestamp theTS = myResult.getTimestamp("sttime");
        if (!myResult.wasNull()) {
            DateTime theTimestamp = SQLUtility.getDatetime(theTS);
            theTSString = theTimestamp.withZoneRetainFields(DateTimeZone.UTC).toString();
        }

        // Voided statement should not be returned
        int isVoided = myResult.getInt("isVoided");
        if (isVoided == 0) {
            statementArray.put(myResult.getInt("statementid"),
                    new XapiStatement(theId, theActor, theVerb, theObject, theResult, theContext, theTSString));
        }
    }

    return statementArray;
}

From source file:com.sos.hibernate.classes.UtcTimeHelper.java

License:Apache License

public static String convertTimeZonesToString(String dateFormat, String fromTimeZone, String toTimeZone,
        DateTime fromDateTime) {//from w  ww  . ja va2  s  . c  om
    DateTimeZone fromZone = DateTimeZone.forID(fromTimeZone);
    DateTimeZone toZone = DateTimeZone.forID(toTimeZone);

    DateTime dateTime = new DateTime(fromDateTime);

    dateTime = dateTime.withZoneRetainFields(fromZone);

    DateTime toDateTime = new DateTime(dateTime).withZone(toZone);

    DateTimeFormatter oFormatter = DateTimeFormat.forPattern("yyyy-MM-dd'T'H:mm:ss.SSSZ");
    DateTimeFormatter oFormatter2 = DateTimeFormat.forPattern(dateFormat);

    DateTime newDate = oFormatter.withOffsetParsed().parseDateTime(toDateTime.toString());

    return oFormatter2.withZone(toZone).print(newDate.getMillis());

}

From source file:com.sos.hibernate.classes.UtcTimeHelper.java

License:Apache License

public static Date convertTimeZonesToDate(String fromTimeZone, String toTimeZone, DateTime fromDateTime) {
    if (fromDateTime == null) {
        return null;
    }//w  w  w  .  ja  va2  s . c  o m
    DateTimeZone fromZone = DateTimeZone.forID(fromTimeZone);
    DateTimeZone toZone = DateTimeZone.forID(toTimeZone);

    DateTime dateTime = new DateTime(fromDateTime);

    dateTime = dateTime.withZoneRetainFields(fromZone);

    DateTime toDateTime = new DateTime(dateTime).withZone(toZone);

    DateTimeFormatter oFormatter = DateTimeFormat.forPattern("yyyy-MM-dd'T'H:mm:ss.SSSZ");
    DateTimeFormatter oFormatter2 = DateTimeFormat.forPattern("yyyy-MM-dd H:mm:ss.ss");

    DateTime newDate = oFormatter.withOffsetParsed().parseDateTime(toDateTime.toString());

    try {
        return new SimpleDateFormat("yyyy-MM-dd H:mm:ss.ss")
                .parse(oFormatter2.withZone(toZone).print(newDate.getMillis()));
    } catch (ParseException e) {
        e.printStackTrace();
        return null;
    }

}

From source file:com.wso2telco.dep.reportingservice.dao.BillingDAO.java

License:Open Source License

/**
 * Convert to local time./*from w w  w . ja v a2s  .c  o m*/
 *
 * @param timeOffset the time offset
 * @param time the time
 * @return the string
 */
public String convertToLocalTime(String timeOffset, String time) {
    Integer offsetValue = Integer.parseInt(timeOffset);

    log.debug("Offset value = " + offsetValue);
    DateTimeZone systemTimeZone = DateTimeZone.getDefault();
    log.debug("system time zone " + systemTimeZone.toString());
    DateTimeFormatter formatter = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss");
    DateTime systemDateTime = formatter.parseDateTime(time);
    log.debug("system date time " + systemDateTime.toString());
    systemDateTime = systemDateTime.withZoneRetainFields(systemTimeZone);
    log.debug("system date time after adding systemtimezone === " + systemDateTime.toString());

    int hours = -1 * offsetValue / 60;
    int minutes = offsetValue % 60;
    minutes = Math.abs(minutes);

    DateTimeZone localTimeZone = DateTimeZone.forOffsetHoursMinutes(hours, minutes);

    log.debug("Local time zone ==== " + localTimeZone.toString());

    DateTime convertedDateTime = systemDateTime.withZone(localTimeZone);

    String convertedDateTimeString = formatter.print(convertedDateTime);

    log.debug("converted time  :" + convertedDateTimeString);

    return convertedDateTimeString;

}

From source file:edu.unc.lib.dl.util.DateTimeUtil.java

License:Apache License

/**
 * Parse a date in any ISO 8601 format. Default TZ is based on Locale.
 *
 * @param isoDate/*from www  .j  a va  2 s.com*/
 *           ISO8601 date/time string with or without TZ offset
 * @return a Joda DateTime object in UTC (call toString() to print)
 */
public static DateTime parseISO8601toUTC(String isoDate) {
    DateTime result = null;
    DateTimeFormatter fmt = ISODateTimeFormat.dateTimeParser().withOffsetParsed();
    // TODO what about preserving the precision of the original?
    DateTime isoDT = fmt.parseDateTime(isoDate);
    if (isoDT.year().get() > 9999) {
        // you parsed my month as part of the year!
        try {
            fmt = DateTimeFormat.forPattern("yyyyMMdd");
            fmt = fmt.withZone(DateTimeZone.forID("America/New_York"));
            isoDT = fmt.parseDateTime(isoDate);
        } catch (IllegalArgumentException e) {
            try {
                fmt = DateTimeFormat.forPattern("yyyyMM");
                fmt = fmt.withZone(DateTimeZone.forID("America/New_York"));
                isoDT = fmt.parseDateTime(isoDate);
            } catch (IllegalArgumentException e1) {
                try {
                    fmt = DateTimeFormat.forPattern("yyyy");
                    fmt = fmt.withZone(DateTimeZone.forID("America/New_York"));
                    isoDT = fmt.parseDateTime(isoDate);
                } catch (IllegalArgumentException ignored) {
                    // I guess we go with first parse?
                }
            }
        }
    }
    result = isoDT.withZoneRetainFields(DateTimeZone.forID("Etc/UTC"));
    return result;
}

From source file:io.druid.sql.calcite.planner.Calcites.java

License:Apache License

/**
 * Calcite expects "DATE" types to be number of days from the epoch to the UTC date matching the local time fields.
 *
 * @param dateTime joda timestamp//from  w w  w .  j a v a 2  s .  c o m
 * @param timeZone session time zone
 *
 * @return Calcite style date
 */
public static int jodaToCalciteDate(final DateTime dateTime, final DateTimeZone timeZone) {
    final DateTime date = dateTime.withZone(timeZone).dayOfMonth().roundFloorCopy();
    return Days.daysBetween(new DateTime(0L, DateTimeZone.UTC), date.withZoneRetainFields(DateTimeZone.UTC))
            .getDays();
}