Android Calendar Format getUniversalDateStamp(Calendar cal)

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

Description

Get current time stamp in universal format
Format: yyyy-mm-ddThh:mm:ssZ
e.g.: 1999-09-09T13:10:40Z

Return

The Date in the format: yyyy-mm-ddThh:mm:ssZ

Declaration

public static String getUniversalDateStamp(Calendar cal) 

Method Source Code

//package com.java2s;

import java.util.Calendar;
import java.util.Date;

public class Main {
    /** /*from w w  w .  j  a  v  a2 s. c o  m*/
     * Get current time stamp in universal format<br>
     * Format: yyyy-mm-ddThh:mm:ssZ<br>
     * e.g.: 1999-09-09T13:10:40Z
     * @return The Date in the format: yyyy-mm-ddThh:mm:ssZ
     */
    public static String getUniversalDateStamp(long time) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(new Date(time));
        return getUniversalDateStamp(cal);
    }

    /** 
     * Get current time stamp in universal format<br>
     * Format: yyyy-mm-ddThh:mm:ssZ<br>
     * e.g.: 1999-09-09T13:10:40Z
     * @return The Date in the format: yyyy-mm-ddThh:mm:ssZ
     */
    public static String getUniversalDateStamp(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        return getUniversalDateStamp(cal);
    }

    /** 
     * Get current time stamp in universal format<br>
     * Format: yyyy-mm-ddThh:mm:ssZ<br>
     * e.g.: 1999-09-09T13:10:40Z
     * @return The Date in the format: yyyy-mm-ddThh:mm:ssZ  
     */
    public static String getUniversalDateStamp(Calendar cal) {
        String year = String.valueOf(cal.get(Calendar.YEAR));
        String month = String.valueOf(cal.get(Calendar.MONTH) + 1);
        if (month.length() == 1) {
            month = "0" + month;
        }
        String day = String.valueOf(cal.get(Calendar.DAY_OF_MONTH));
        if (day.length() == 1) {
            day = "0" + day;
        }
        String hour = String.valueOf(cal.get(Calendar.HOUR_OF_DAY));
        if (hour.length() == 1) {
            hour = "0" + hour;
        }
        String minute = String.valueOf(cal.get(Calendar.MINUTE));
        if (minute.length() == 1) {
            minute = "0" + minute;
        }
        String second = String.valueOf(cal.get(Calendar.SECOND));
        if (second.length() == 1) {
            second = "0" + second;
        }
        String dateStamp = year + "-" + month + "-" + day + "T" + hour
                + ":" + minute + ":" + second + "Z";
        return dateStamp;
    }
}

Related

  1. formatReminder(Context ctx, Calendar date)
  2. formatTM(Calendar c, StringBuffer sb)
  3. formatTaskWarrior(Calendar c)
  4. formateCalDav(Calendar c)
  5. formateCalDavDue(Calendar c)
  6. getFormattedTime(Context context, Calendar time)
  7. getFormattedTime(Context context, Calendar time)
  8. calendar2string(Calendar calendar, String formatString)
  9. format(Calendar cal, String format, Locale locale)