Java Calendar Calculate getTimeStrWithMillisecond(Calendar timeInstance)

Here you can find the source of getTimeStrWithMillisecond(Calendar timeInstance)

Description

get Time Str With Millisecond

License

Open Source License

Declaration

public static String getTimeStrWithMillisecond(Calendar timeInstance) 

Method Source Code


//package com.java2s;
import java.util.*;

public class Main {
    public static String getTimeStrWithMillisecond(Calendar timeInstance) {
        Calendar time = timeInstance;
        int field;
        String year = "", month = "", day = "", hour = "", minute = "", second = "", millisecond = "";
        String timeStr = "";

        year = Integer.toString(time.get(Calendar.YEAR));
        field = time.get(Calendar.MONTH) + 1;
        if (field < 10) {
            month = "0";
        }//from  w w  w .ja v a  2  s  . co  m
        month = month + Integer.toString(field);
        field = time.get(Calendar.DAY_OF_MONTH);
        if (field < 10) {
            day = "0";
        }
        day = day + Integer.toString(field);
        field = time.get(Calendar.HOUR_OF_DAY);
        if (field < 10) {
            hour = "0";
        }
        hour = hour + Integer.toString(field);
        field = time.get(Calendar.MINUTE);
        if (field < 10) {
            minute = "0";
        }
        minute = minute + Integer.toString(field);
        field = time.get(Calendar.SECOND);
        if (field < 10) {
            second = "0";
        }
        second = second + Integer.toString(field);
        field = time.get(Calendar.MILLISECOND);
        if (field < 10) {
            millisecond = "00";
        } else if (field < 100) {
            millisecond = "0";
        }
        millisecond = millisecond + Integer.toString(field);

        int zoneOffset = time.get(Calendar.ZONE_OFFSET);
        int dstOffset = time.get(Calendar.DST_OFFSET);
        int offSec = (zoneOffset + dstOffset) / 1000;
        //    int offSec = time.get(Calendar.ZONE_OFFSET)/1000;
        String pm = null, offMinStr = null, offHrStr = null;

        if (offSec >= 0) {
            pm = "+";
        } else {
            pm = "-";
        }
        offSec = Math.abs(offSec);
        int offMin = (offSec % 3600) / 60;
        int offHr = offSec / 3600;

        if (offMin < 10) {
            offMinStr = "0" + Integer.toString(offMin);
        } else {
            offMinStr = Integer.toString(offMin);
        }
        if (offHr < 10) {
            offHrStr = "0" + Integer.toString(offHr);
        } else {
            offHrStr = Integer.toString(offHr);
        }
        timeStr = year + "-" + month + "-" + day + "T" + hour + ":" + minute + ":" + second + "." + millisecond + pm
                + offHrStr + ":" + offMinStr;
        return timeStr;
    }
}

Related

  1. getCalendar(long millis)
  2. getGregorianCalendar(String yyyymmdd)
  3. getLongTime(Calendar cal)
  4. getTimeStr3(Calendar timeInstance)
  5. getTimeString(Calendar cal)
  6. isAfterWorkTime(Calendar start)
  7. isInWorkTime(Calendar start)
  8. isSameInstant(Calendar cal1, Calendar cal2)
  9. isSameLocalTime(Calendar cal1, Calendar cal2)