Java Day End getDateTimeString(boolean appendMillis)

Here you can find the source of getDateTimeString(boolean appendMillis)

Description

get Date Time String

License

Open Source License

Declaration

public static String getDateTimeString(boolean appendMillis) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.util.Calendar;

public class Main {
    public static String getDateTimeString(boolean appendMillis) {
        StringBuilder b = new StringBuilder();
        Calendar cal = Calendar.getInstance();
        b.append(getFixedString(cal.get(Calendar.YEAR), 4));
        b.append(getFixedString(cal.get(Calendar.MONTH) + 1, 2));
        b.append(getFixedString(cal.get(Calendar.DAY_OF_MONTH), 2));
        b.append("_");
        b.append(getFixedString(cal.get(Calendar.HOUR_OF_DAY), 2));
        b.append(getFixedString(cal.get(Calendar.MINUTE), 2));
        b.append(getFixedString(cal.get(Calendar.SECOND), 2));
        if (appendMillis) {
            b.append("_");
            b.append(cal.get(Calendar.MILLISECOND));
        }//from  w ww.  j av a2 s  . c o m
        return b.toString();
    }

    public static String getFixedString(int number, int digits) {
        StringBuilder b = new StringBuilder();
        String numberString = Integer.toString(number);
        int toAdd = digits - numberString.length();
        for (int i = 0; i < toAdd; i++) {
            b.append('0');
        }
        return b.append(numberString).toString();
    }
}

Related

  1. getDateEnd(java.util.Date d)
  2. getDateEndDay()
  3. getDateInterval4EndDate(Date date, int hourModify)
  4. getDateList(Date begin, Date end)
  5. getDatesBetweenTwoDate(Date beginDate, Date endDate)
  6. getDefaultEndDateForLogQuery()
  7. getDistDates(Date startDate, Date endDate)
  8. getEndDate()
  9. getEndDate(Date beginDate, int resolution)