org.myorganisation.myproject.quickstart.backend.restful.war.DateTimeUtilities.java Source code

Java tutorial

Introduction

Here is the source code for org.myorganisation.myproject.quickstart.backend.restful.war.DateTimeUtilities.java

Source

/*
 * #%L
 * Nazgul Project: quickstart-restful-war
 * %%
 * Copyright (C) 2015 MyOrganisation
 * %%
 * Licensed under the jGuru Europe AB license (the "License"), based
 * on Apache License, Version 2.0; you may not use this file except
 * in compliance with the License.
 * 
 * You may obtain a copy of the License at
 * 
 *       http://www.jguru.se/licenses/jguruCorporateSourceLicense-2.0.txt
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * #L%
 */
package org.myorganisation.myproject.quickstart.backend.restful.war;

import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;

/**
 * Utility class containing algorithms for converting Strings to Dates and vice versa.
 *
 * @author <a href="mailto:lj@jguru.se">Lennart J&ouml;relid</a>, jGuru Europe AB
 */
public final class DateTimeUtilities {

    // Internal state
    private static DateTimeUtilities instance;
    private DateTimeZone dateTimeZone;
    private String dateTimePattern = "yyyy-MM-dd";

    /*
     * y       year                         year          1996
     * M       month of year                month         July; Jul; 07
     * d       day of month                 number        10
     */
    private DateTimeFormatter formatter = DateTimeFormat.forPattern(dateTimePattern);

    /**
     * Compound constructor creating a DateTimeUtilities instance wrapping the supplied data.
     *
     * @param tz The non-null DateTimeZone in use.
     */
    private DateTimeUtilities(final DateTimeZone tz) {
        this.dateTimeZone = tz;
    }

    /**
     * Factory method getting the singleton DateTimeUtilities instance, or creating it if nonexistent.
     *
     * @return The singleton DateTimeUtilities instance.
     */
    public static DateTimeUtilities getInstance() {
        if (instance == null) {
            instance = new DateTimeUtilities(DateTimeZone.UTC);
        }
        return instance;
    }

    /**
     * Assigns the timezone of the DateTimeUtilities singleton instance, creating it if necessary.
     *
     * @param timezone The ID of the DateTimeZone to set.
     * @return The singleton DateTimeUtilities instance.
     */
    public static DateTimeUtilities setTimeZone(final String timezone) {

        final DateTimeZone dateTimeZone = DateTimeZone.forID(timezone);
        if (instance == null) {
            instance = new DateTimeUtilities(dateTimeZone);
        } else {
            instance.dateTimeZone = dateTimeZone;
        }

        // All done.
        return instance;
    }

    /**
     * Converts the supplied timestamp (given as a String), returning a DateTime if possible.
     *
     * @param timestampAsString      The DateTime string form
     * @param nullTimestampYieldsNow if {@code true}, then a null value in the timestampAsString argument will
     *                               retrieve a DateTime for now.
     * @return A DateTime parsed from the supplied timestampAsString.
     */
    public DateTime convert(final String timestampAsString, boolean nullTimestampYieldsNow) {

        if (nullTimestampYieldsNow && timestampAsString == null) {
            return new DateTime();
        }

        // All done.
        return DateTime.parse(timestampAsString, formatter).withZone(dateTimeZone);
    }
}