Example usage for org.joda.time DateTimeZone forTimeZone

List of usage examples for org.joda.time DateTimeZone forTimeZone

Introduction

In this page you can find the example usage for org.joda.time DateTimeZone forTimeZone.

Prototype

public static DateTimeZone forTimeZone(TimeZone zone) 

Source Link

Document

Gets a time zone instance for a JDK TimeZone.

Usage

From source file:org.apache.streams.data.util.RFC3339Utils.java

License:Apache License

public static String format(DateTime toFormat, TimeZone tz) {
    return LOCAL_SUB_SECOND_FMT.withZone(DateTimeZone.forTimeZone(tz)).print(toFormat.getMillis());
}

From source file:org.apache.wicket.datetime.DateConverter.java

License:Apache License

/**
 * @see org.apache.wicket.util.convert.IConverter#convertToObject(java.lang.String,
 *      java.util.Locale)/*www  . jav a 2  s . c  om*/
 */
public Date convertToObject(String value, Locale locale) {
    if (Strings.isEmpty(value)) {
        return null;
    }

    DateTimeFormatter format = getFormat(locale);
    if (format == null) {
        throw new IllegalStateException("format must be not null");
    }

    if (applyTimeZoneDifference) {
        TimeZone zone = getClientTimeZone();
        DateTime dateTime = null;

        // set time zone for client
        format = format.withZone(getTimeZone());

        try {
            // parse date retaining the time of the submission
            dateTime = format.parseDateTime(value);
        } catch (RuntimeException e) {
            throw new ConversionException(e);
        }
        // apply the server time zone to the parsed value
        if (zone != null) {
            dateTime = dateTime.withZoneRetainFields(DateTimeZone.forTimeZone(zone));
        }

        return dateTime.toDate();
    } else {
        try {
            DateTime date = format.parseDateTime(value);
            return date.toDate();
        } catch (RuntimeException e) {
            throw new ConversionException(e);
        }
    }
}

From source file:org.apache.wicket.datetime.DateConverter.java

License:Apache License

/**
 * @see org.apache.wicket.util.convert.IConverter#convertToString(java.lang.Object,
 *      java.util.Locale)/*from   w  ww  .  j  ava  2  s . c  o m*/
 */
public String convertToString(Date value, Locale locale) {
    DateTime dt = new DateTime((value).getTime(), getTimeZone());
    DateTimeFormatter format = getFormat(locale);

    if (applyTimeZoneDifference) {
        TimeZone zone = getClientTimeZone();
        if (zone != null) {
            // apply time zone to formatter
            format = format.withZone(DateTimeZone.forTimeZone(zone));
        }
    }
    return format.print(dt);
}

From source file:org.apache.wicket.extensions.yui.calendar.DateTimeField.java

License:Apache License

/**
 * Sets the converted input, which is an instance of {@link Date}, possibly null. It combines
 * the inputs of the nested date, hours, minutes and am/pm fields and constructs a date from it.
 * <p>/*  w w  w.j  a  v  a  2s . com*/
 * Note that overriding this method is a better option than overriding {@link #updateModel()}
 * like the first versions of this class did. The reason for that is that this method can be
 * used by form validators without having to depend on the actual model being updated, and this
 * method is called by the default implementation of {@link #updateModel()} anyway (so we don't
 * have to override that anymore).
 */
@Override
protected void convertInput() {
    try {
        // Get the converted input values
        Date dateFieldInput = dateField.getConvertedInput();
        Integer hoursInput = hoursField.getConvertedInput();
        Integer minutesInput = minutesField.getConvertedInput();
        AM_PM amOrPmInput = amOrPmChoice.getConvertedInput();

        if (dateFieldInput == null) {
            return;
        }

        // Get year, month and day ignoring any timezone of the Date object
        Calendar cal = Calendar.getInstance();
        cal.setTime(dateFieldInput);
        int year = cal.get(Calendar.YEAR);
        int month = cal.get(Calendar.MONTH) + 1;
        int day = cal.get(Calendar.DAY_OF_MONTH);
        int hours = (hoursInput == null ? 0 : hoursInput % 24);
        int minutes = (minutesInput == null ? 0 : minutesInput);

        // Use the input to create a date object with proper timezone
        MutableDateTime date = new MutableDateTime(year, month, day, hours, minutes, 0, 0,
                DateTimeZone.forTimeZone(getClientTimeZone()));

        // Adjust for halfday if needed
        if (use12HourFormat()) {
            int halfday = (amOrPmInput == AM_PM.PM ? 1 : 0);
            date.set(DateTimeFieldType.halfdayOfDay(), halfday);
            date.set(DateTimeFieldType.hourOfHalfday(), hours % 12);
        }

        // The date will be in the server's timezone
        setConvertedInput(new Date(date.getMillis()));
    } catch (RuntimeException e) {
        DateTimeField.this.error(e.getMessage());
        invalid();
    }
}

From source file:org.datavec.api.transform.metadata.TimeMetaData.java

License:Apache License

/**
 * @param timeZone     Timezone for this column. Typically used for parsing and some transforms
 * @param minValidTime Minimum valid time, in milliseconds (timestamp format). If null: no restriction
 * @param maxValidTime Maximum valid time, in milliseconds (timestamp format). If null: no restriction
 *//*from  ww w . j a  v a  2 s  . c o m*/
public TimeMetaData(String name, TimeZone timeZone, Long minValidTime, Long maxValidTime) {
    super(name);
    this.timeZone = DateTimeZone.forTimeZone(timeZone);
    this.minValidTime = minValidTime;
    this.maxValidTime = maxValidTime;
}

From source file:org.datavec.api.transform.transform.time.StringToTimeTransform.java

License:Apache License

/**
 * @param columnName   Name of the String column
 * @param timeFormat   Time format, as per http://www.joda.org/joda-time/apidocs/org/joda/time/format/DateTimeFormat.html
 * @param timeZone     Timezone for time parsing
 * @param minValidTime Min valid time (epoch millisecond format). If null: no restriction in min valid time
 * @param maxValidTime Max valid time (epoch millisecond format). If null: no restriction in max valid time
 *//*ww w  .  j  a  v a 2 s.c  om*/
public StringToTimeTransform(@JsonProperty("columnName") String columnName,
        @JsonProperty("timeFormat") String timeFormat, @JsonProperty("timeZone") TimeZone timeZone,
        @JsonProperty("minValidTime") Long minValidTime, @JsonProperty("maxValidTime") Long maxValidTime) {
    this(columnName, timeFormat, DateTimeZone.forTimeZone(timeZone), minValidTime, maxValidTime);
}

From source file:org.ddialliance.ddiftp.util.Translator.java

License:Open Source License

/**
 * Format an ISO8601 time string defined by into a Calendar<BR>
 * Defined by '-'? yyyy '-' mm '-' dd 'T' hh ':' mm ':' ss ('.' s+)?
 * (zzzzzz)?//w  w w.  ja va  2  s  .co m
 * 
 * @see http://www.w3.org/TR/xmlschema-2/#dateTime
 * @param time
 *            string
 * @return calendar
 * @throws DDIFtpException
 */
public static Calendar formatIso8601DateTime(String time) throws DDIFtpException {
    // yyyy-MM-dd'T'HH:mm:ss.SSSZZ full format
    DateTimeFormatter fmt = ISODateTimeFormat.dateTime();
    try {
        DateTime dateTime = fmt.parseDateTime(time);
        return dateTime.toCalendar(getLocale());
    } catch (IllegalArgumentException e) {
        try {
            // yyyy-MM-dd'T'HH:mm:ssZZ with out millisecond
            fmt = ISODateTimeFormat.dateTimeNoMillis();
            fmt.withLocale(getLocale());
            fmt.withZone(DateTimeZone.forTimeZone(getTimeZone()));
            DateTime dateTime = fmt.parseDateTime(time);
            return dateTime.toCalendar(getLocale());
        } catch (IllegalArgumentException e1) {
            try {
                // yyyy-MM-dd'T'HH:mm:ss.SS with out time zone
                fmt = ISODateTimeFormat.dateHourMinuteSecondFraction();
                fmt.withLocale(getLocale());
                fmt.withZone(DateTimeZone.forTimeZone(getTimeZone()));
                DateTime dateTime = fmt.parseDateTime(time);
                return dateTime.toCalendar(getLocale());
            } catch (Exception e2) {
                try {
                    // yyyy-MM-dd'T'HH:mm:ss with out millisecond and time
                    // zone
                    fmt = ISODateTimeFormat.dateHourMinuteSecond();
                    fmt.withLocale(getLocale());
                    fmt.withZone(DateTimeZone.forTimeZone(getTimeZone()));
                    DateTime dateTime = fmt.parseDateTime(time);
                    return dateTime.toCalendar(getLocale());
                } catch (IllegalArgumentException e3) {
                    try {
                        fmt = ISODateTimeFormat.dateParser();
                        fmt.withLocale(getLocale());
                        fmt.withZone(DateTimeZone.forTimeZone(getTimeZone()));
                        DateTime dateTime = fmt.parseDateTime(time);
                        return dateTime.toCalendar(getLocale());
                    } catch (Exception e4) {
                        throw new DDIFtpException("translate.timeformat.error",
                                new Object[] { time,
                                        "'-'? yyyy '-' mm '-' dd 'T' hh ':' mm ':' ss ('.' s+)? (zzzzzz)?" },
                                e);
                    }
                }
            }
        }
    }
}

From source file:org.ddialliance.ddiftp.util.Translator.java

License:Open Source License

/**
 * Format time in milli seconds to an ISO8601 time string<BR>
 * Defined by '-'? yyyy '-' mm '-' dd 'T' hh ':' mm ':' ss ('.' s+)?
 * (zzzzzz)?/*  ww  w .ja v a  2 s.c  o  m*/
 * 
 * @see http://www.w3.org/TR/xmlschema-2/#dateTime
 * @param time
 *            in milli seconds
 * @return ISO8601 time string
 * @throws DDIFtpException
 */
public static String formatIso8601DateTime(long time) {
    DateTimeFormatter fmt = ISODateTimeFormat.dateTime();
    fmt.withLocale(getLocale());
    fmt.withZone(DateTimeZone.forTimeZone(getTimeZone()));
    return fmt.print(time);
}

From source file:org.efaps.cli.rest.ImportCICall.java

License:Apache License

/**
 * Gets the file information./*from  w  w  w.jav  a 2s  . c o  m*/
 *
 * @param _file the _file
 * @return the file information
 */
protected String[] getFileInformation(final File _file) {
    final String[] ret = new String[2];

    try {
        final Repository repo = new FileRepository(evalGitDir(_file));

        final ObjectId lastCommitId = repo.resolve(Constants.HEAD);

        final PlotCommitList<PlotLane> plotCommitList = new PlotCommitList<PlotLane>();
        final PlotWalk revWalk = new PlotWalk(repo);

        final RevCommit root = revWalk.parseCommit(lastCommitId);
        revWalk.markStart(root);
        revWalk.setTreeFilter(AndTreeFilter.create(
                PathFilter.create(_file.getPath().replaceFirst(repo.getWorkTree().getPath() + "/", "")),
                TreeFilter.ANY_DIFF));
        plotCommitList.source(revWalk);
        plotCommitList.fillTo(2);
        final PlotCommit<PlotLane> commit = plotCommitList.get(0);
        if (commit != null) {
            final PersonIdent authorIdent = commit.getAuthorIdent();
            final Date authorDate = authorIdent.getWhen();
            final TimeZone authorTimeZone = authorIdent.getTimeZone();
            final DateTime dateTime = new DateTime(authorDate.getTime(),
                    DateTimeZone.forTimeZone(authorTimeZone));
            ret[1] = dateTime.toString();
            ret[0] = commit.getId().getName();
        } else {
            ret[1] = new DateTime().toString();
            ret[0] = "UNKNOWN";
        }
    } catch (final RevisionSyntaxException | IOException e) {
        e.printStackTrace();
    }
    return ret;
}

From source file:org.efaps.eclipse.rest.RestClient.java

License:Apache License

protected String[] getFileInformation(final File _file) {
    final String[] ret = new String[2];

    try {/*w w  w . j a  v  a  2 s . c  om*/
        final Repository repo = new FileRepository(evalGitDir(_file));

        final ObjectId lastCommitId = repo.resolve(Constants.HEAD);

        final PlotCommitList<PlotLane> plotCommitList = new PlotCommitList<PlotLane>();
        final PlotWalk revWalk = new PlotWalk(repo);

        final RevCommit root = revWalk.parseCommit(lastCommitId);
        revWalk.markStart(root);
        revWalk.setTreeFilter(AndTreeFilter.create(
                PathFilter.create(_file.getPath().replaceFirst(repo.getWorkTree().getPath() + "/", "")),
                TreeFilter.ANY_DIFF));
        plotCommitList.source(revWalk);
        plotCommitList.fillTo(2);
        final PlotCommit<PlotLane> commit = plotCommitList.get(0);
        if (commit != null) {
            final PersonIdent authorIdent = commit.getAuthorIdent();
            final Date authorDate = authorIdent.getWhen();
            final TimeZone authorTimeZone = authorIdent.getTimeZone();
            final DateTime dateTime = new DateTime(authorDate.getTime(),
                    DateTimeZone.forTimeZone(authorTimeZone));
            ret[1] = dateTime.toString();
            ret[0] = commit.getId().getName();
        } else {
            ret[1] = new DateTime().toString();
            ret[0] = "UNKNOWN";
        }
    } catch (RevisionSyntaxException | IOException e) {
        e.printStackTrace();
    }
    return ret;
}