Android Date Format getUniversalDateStamp(Date date)

Here you can find the source of getUniversalDateStamp(Date date)

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(Date date) 

Method Source Code

//package com.java2s;

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

public class Main {
    /** //from  ww w  .ja  va 2 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. getFormatDateTommorrow(String sourceDate, String format)
  2. getFormatShortTime(Date date)
  3. getFormatTime(Date date)
  4. getFormatTomorrow(String format)
  5. getFormatYestoday(String format)
  6. getUtcTimeStringFromDate(Date date)
  7. timestampToISO8601(Date aDate)
  8. toDateString(Date value)
  9. toDateTimeString(Date value)