Java Time Format getFormattedTime(final Calendar calendar)

Here you can find the source of getFormattedTime(final Calendar calendar)

Description

Function to return formatted time

License

Open Source License

Parameter

Parameter Description
calendar calendar object which holds the current time that needs to be formatted.

Return

the String format of the date and time that the calendar object contained

Declaration

public static String getFormattedTime(final Calendar calendar) 

Method Source Code


//package com.java2s;

import java.text.SimpleDateFormat;
import java.util.Calendar;

import java.util.TimeZone;

public class Main {
    /**/*w  ww.  j  a v  a  2 s .c o  m*/
     * Function to return formatted time 
     * @param calendar
     *  calendar object which holds the current time that needs to be formatted.
     * @return
     *  the String format of the date and time that the calendar object contained
     */
    public static String getFormattedTime(final Calendar calendar) {
        SimpleDateFormat format = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
        format.setTimeZone(TimeZone.getTimeZone("UTC"));

        return format.format(calendar.getTime());
    }

    /**
     * Get the standard formatted time string for the given time in milliseconds
     */
    public static String getFormattedTime(long timeInMillis) {
        return getFormattedTime(getCalendarForTime(timeInMillis));
    }

    /**
     * Function to return a calendar object holding the UTC representation of the current time.
     * @param timeInMilliseconds
     *  the time in milliseconds from the Jan 1 1970 epoch that is to be used as the time to set 
     *  the calendar to.
     * @return
     *  the calendar object which represents the time that was passed in in UTC
     */
    public static Calendar getCalendarForTime(final long timeInMilliseconds) {
        Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
        calendar.setTimeInMillis(timeInMilliseconds);

        return calendar;
    }
}

Related

  1. getFormattedDuration(long fromTime)
  2. getFormattedFileTime(File f, String format)
  3. getFormattedTime()
  4. getFormattedTime(Date date)
  5. getFormattedTime(Date date)
  6. getFormattedTime(final Date date)
  7. getFormattedTimeFilesafe(long t)
  8. getFormattedTimeString(double s)
  9. getFormatTime(Date date)