Java TimeUnit Usage createTimeString(int year, int month, int day, int hours, int minutes, int seconds, String timezoneID)

Here you can find the source of createTimeString(int year, int month, int day, int hours, int minutes, int seconds, String timezoneID)

Description

Creates a timestamp conform to the ISO 8601 specification, supported its single information bits.

License

Apache License

Parameter

Parameter Description
year The year for the timestamp.
month The month for the timestamp.
day The day for the timestamp.
hours The hours for the timestamp.
minutes The minutes for the timestamp.
seconds The seconds for the timestamp.
timezoneID The timezone for the timestamp.

Return

A textual representation of the timestamp defined by the supported parameters.

Declaration

public static String createTimeString(int year, int month, int day, int hours, int minutes, int seconds,
        String timezoneID) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import org.joda.time.DateTimeZone;

import java.util.concurrent.TimeUnit;

public class Main {
    /**//from w  ww .j  a v  a2s  . com
     * Creates a timestamp conform to the ISO 8601 specification, supported its single information bits.
     *
     * @param year       The year for the timestamp.
     * @param month      The month for the timestamp.
     * @param day        The day for the timestamp.
     * @param hours      The hours for the timestamp.
     * @param minutes    The minutes for the timestamp.
     * @param seconds    The seconds for the timestamp.
     * @param timezoneID The timezone for the timestamp.
     * @return A textual representation of the timestamp defined by the supported parameters.
     */
    public static String createTimeString(int year, int month, int day, int hours, int minutes, int seconds,
            String timezoneID) {
        StringBuilder builder = new StringBuilder();
        builder.append(Integer.toString(year)).append("-").append(Integer.toString(month)).append("-")
                .append(Integer.toString(day)).append("T");

        if (hours < 10) {
            builder.append(0);
        }
        builder.append(Integer.toString(hours));

        builder.append(":");

        if (minutes < 10) {
            builder.append(0);
        }
        builder.append(Integer.toString(minutes));

        builder.append(":");

        if (seconds < 10) {
            builder.append(0);
        }
        builder.append(Integer.toString(seconds));

        // Timezone
        builder.append(createTimezoneString(timezoneID));

        return builder.toString();
    }

    /**
     * Taking a timezone ID, this method returns the offset in hours.
     *
     * @param timezoneID The ID of the timezone to support the hours offset for.
     * @return A textual representation of the hours offset for the supported timezone.
     */
    private static String createTimezoneString(String timezoneID) {
        StringBuilder builder = new StringBuilder();

        DateTimeZone timeZone = DateTimeZone.forID(timezoneID);
        long offsetInMilliseconds = timeZone.toTimeZone().getRawOffset();
        long offsetHours = TimeUnit.MILLISECONDS.toHours(offsetInMilliseconds);

        if (offsetHours == 0) {
            builder.append("Z");
        } else {
            if (offsetHours < 0) {
                // Negative
                builder.append("-");

                if (offsetHours < -9) {
                    builder.append(Math.abs(offsetHours));
                } else {
                    builder.append(0).append(Math.abs(offsetHours));
                }
            } else {
                // Positive
                if (offsetHours > 9) {
                    builder.append(offsetHours);
                } else {
                    builder.append(0).append(offsetHours);
                }
            }
            builder.append(":00");
        }

        return builder.toString();
    }
}

Related

  1. convertMilisecondsToDetail(long ms)
  2. convertMillis(long millis)
  3. convertMillisecondsToHHMMSS(long milliseconds)
  4. convertNanosecondTimespanToHumanReadableFormat(long aTimespan, boolean aShortFormat, boolean aLongFormat)
  5. convertNanoToSeconds(long nanoTime)
  6. createTimezoneString(String timezoneID)
  7. currentMillisFromNanotime()
  8. dateDiff(Date d1, Date d2)
  9. dateStart(final Calendar c)