Android Long to Date Convert getUniversalDateStamp(long time)

Here you can find the source of getUniversalDateStamp(long time)

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(long time) 

Method Source Code

//package com.java2s;

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

public class Main {
    /** //from   ww  w.  ja  v a  2s  . com
     * 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. getTimestampDatetime(long milliseconds)
  2. getUTCTimeFromElapsedTime(long elapsedTime)
  3. getUTCTimeFromLocalMillis(long localMillis)
  4. getUTCandRelativeDateFromElapsedTime( long elapsedTime)
  5. getUTCandRelativeDateFromElapsedTime( long elapsedTime)
  6. getUpdateDateStr(long time)
  7. getUpdateTimeStr(long time)
  8. getYear(long dateTimeMillis)
  9. hrColMin(final long time, final boolean alwaysIncludeHours)