Java Calendar Millisecond toStringDateTimeMillisecond(Calendar calendar)

Here you can find the source of toStringDateTimeMillisecond(Calendar calendar)

Description

to String Date Time Millisecond

License

Open Source License

Parameter

Parameter Description
calendar a parameter

Return

String ("20130101010101001")

Declaration

public static String toStringDateTimeMillisecond(Calendar calendar) 

Method Source Code

//package com.java2s;
/*/*from   ww w.ja va 2  s  .c  o  m*/
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

import java.util.Calendar;

public class Main {
    private static final int BUDDHIST_YEAR = 543;

    /**
     * @param calendar
     * @return String ("20130101010101001")
     * @example String dateUI =
     * DateUtils.convertCalendarToStringDateTimeMillisecond(
     * DateUtils.getCurrentDateCalendarEng() );
     */
    public static String toStringDateTimeMillisecond(Calendar calendar) {
        String yyyy = String.valueOf(calendar.get(Calendar.YEAR) + BUDDHIST_YEAR);
        String mm = String.valueOf(calendar.get(Calendar.MONTH) + 1);
        String dd = String.valueOf(calendar.get(Calendar.DAY_OF_MONTH));
        String hh = String.valueOf(calendar.get(Calendar.HOUR_OF_DAY));
        String nn = String.valueOf(calendar.get(Calendar.MINUTE));
        String ss = String.valueOf(calendar.get(Calendar.SECOND));
        String ms = String.valueOf(calendar.get(Calendar.MILLISECOND));
        if (mm.length() < 2) {
            mm = "0" + mm;
        }
        if (dd.length() < 2) {
            dd = "0" + dd;
        }
        if (hh.length() < 2) {
            hh = "0" + hh;
        }
        if (nn.length() < 2) {
            nn = "0" + nn;
        }
        if (ss.length() < 2) {
            ss = "0" + ss;
        }
        if (ms.length() == 1) {
            ms = "00" + ms;
        } else if (ms.length() == 2) {
            ms = "0" + ms;
        }

        return yyyy + mm + dd + hh + nn + ss + ms;
    }
}

Related

  1. setCalendar(Calendar calendar, int wholeDays, int millisecondsInDay, boolean use1904windowing, boolean roundSeconds)
  2. setCalendarTime(Calendar inCalendar, int hr, int min, int sec, int milliSec)
  3. setTimeInMillis(Calendar _calendar, long _timemillis)
  4. toCalendar(final long millis)
  5. toCalendar(long millis)
  6. translateToMilliseconds(Calendar calendar)