Java Calendar Time toTimeStamp(Calendar cal)

Here you can find the source of toTimeStamp(Calendar cal)

Description

Converts a Calendar object into a timestamp string in ISO 8601 format.

License

Open Source License

Declaration

public static String toTimeStamp(Calendar cal) 

Method Source Code

//package com.java2s;
// See LICENSE.txt for license information

import java.util.Calendar;

public class Main {
    /**//from  ww w .  ja va  2  s  . c om
     * Converts a Calendar object into a timestamp string in ISO 8601 format.
     */
    public static String toTimeStamp(Calendar cal) {
        if (cal == null) {
            cal = Calendar.getInstance();
        }

        StringBuilder sb = new StringBuilder();
        sb.append(String.format("%04d", cal.get(Calendar.YEAR)));
        sb.append('-').append(String.format("%02d", cal.get(Calendar.MONTH) + 1));
        sb.append('-').append(String.format("%02d", cal.get(Calendar.DAY_OF_MONTH)));
        sb.append('T').append(String.format("%02d", cal.get(Calendar.HOUR_OF_DAY)));
        sb.append(':').append(String.format("%02d", cal.get(Calendar.MINUTE)));
        sb.append(':').append(String.format("%02d", cal.get(Calendar.SECOND)));
        int ofs = cal.get(Calendar.ZONE_OFFSET);
        if (ofs != 0) {
            char sign = (ofs < 0) ? '-' : '+';
            ofs = Math.abs(ofs);
            int ofsHour = ofs / 3600000;
            int ofsMin = (ofs / 60000) % 60;
            sb.append(sign).append(String.format("%02d", ofsHour));
            sb.append(':').append(String.format("%02d", ofsMin));
        }

        return sb.toString();
    }
}

Related

  1. time2InicioDelDia(Calendar fecha)
  2. timestamp17ToCalendar(String timestamp17String)
  3. timestamp2Calendar(long timestamp)
  4. timestampToCalendar(String timestamp)
  5. toDefaultTimeZone(Calendar calendar)
  6. toTimeString(final Calendar calendar)
  7. toUnixTime(Calendar timestamp)
  8. toVonTime(final Calendar cal)
  9. unixToCalendar(long unixTime)