Java Calendar Calculate encode(Calendar calendar, boolean encodeTzOffset, StringBuilder buf)

Here you can find the source of encode(Calendar calendar, boolean encodeTzOffset, StringBuilder buf)

Description

Converts a Java Calendar into a DSA encoded timestamp.

License

Apache License

Parameter

Parameter Description
calendar The calendar representing the timestamp to encode.
encodeTzOffset Whether or not to encode the timezone offset.
buf The buffer to append the encoded timestamp and return, can be null.

Return

The buf argument, or if that was null, a new StringBuilder.

Declaration

public static StringBuilder encode(Calendar calendar, boolean encodeTzOffset, StringBuilder buf) 

Method Source Code

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

import java.util.*;

public class Main {
    public static final int MILLIS_MINUTE = 60 * 1000;
    public static final int MILLIS_HOUR = 60 * MILLIS_MINUTE;

    /**//from  ww w . j a  va2s . co m
     * Converts a Java Calendar into a DSA encoded timestamp.  DSA encoding is based
     * on ISO 8601 but allows the timezone offset to not be specified.
     * @param calendar The calendar representing the timestamp to encode.
     * @param encodeTzOffset Whether or not to encode the timezone offset.
     * @param buf The buffer to append the encoded timestamp and return, can be null.
     * @return The buf argument, or if that was null, a new StringBuilder.
     */
    public static StringBuilder encode(Calendar calendar, boolean encodeTzOffset, StringBuilder buf) {
        if (buf == null) {
            buf = new StringBuilder();
        }
        long millis = calendar.getTimeInMillis();
        int tmp = calendar.get(Calendar.YEAR);
        buf.append(tmp).append('-');
        //month
        tmp = calendar.get(Calendar.MONTH) + 1;
        if (tmp < 10)
            buf.append('0');
        buf.append(tmp).append('-');
        //date
        tmp = calendar.get(Calendar.DAY_OF_MONTH);
        if (tmp < 10)
            buf.append('0');
        buf.append(tmp).append('T');
        //hour
        tmp = calendar.get(Calendar.HOUR_OF_DAY);
        if (tmp < 10)
            buf.append('0');
        buf.append(tmp).append(':');
        //minute
        tmp = calendar.get(Calendar.MINUTE);
        if (tmp < 10)
            buf.append('0');
        buf.append(tmp).append(':');
        //second
        tmp = calendar.get(Calendar.SECOND);
        if (tmp < 10)
            buf.append('0');
        buf.append(tmp).append('.');
        //millis
        tmp = calendar.get(Calendar.MILLISECOND);
        if (tmp < 10)
            buf.append('0');
        if (tmp < 100)
            buf.append('0');
        buf.append(tmp);
        if (encodeTzOffset) {
            int offset = calendar.getTimeZone().getOffset(millis);
            if (offset == 0) {
                buf.append('Z');
            } else {
                int hrOff = Math.abs(offset / MILLIS_HOUR);
                int minOff = Math.abs((offset % MILLIS_HOUR) / MILLIS_MINUTE);
                if (offset < 0)
                    buf.append('-');
                else
                    buf.append('+');
                if (hrOff < 10)
                    buf.append('0');
                buf.append(hrOff);
                buf.append(':');
                if (minOff < 10)
                    buf.append('0');
                buf.append(minOff);
            }
        }
        return buf;
    }
}

Related

  1. calDateOnly(Calendar iniCal)
  2. calendarIterator(final int iterateType, final Calendar startCal, final Calendar endCal)
  3. cloneCalendar(Calendar in)
  4. copyCalendar(final Calendar calendar, final boolean stripTime)
  5. decode(String timestamp, Calendar calendar)
  6. equals(Calendar calendar1, Calendar calendar2)
  7. FormatTime(Calendar p_time, boolean p_showMilliseconds)
  8. getCalendar(long millis)
  9. getGregorianCalendar(String yyyymmdd)