Java Date Convert dateToString(Date date)

Here you can find the source of dateToString(Date date)

Description

Method dateToString.

License

Open Source License

Parameter

Parameter Description
date Date

Return

String

Declaration

public static String dateToString(Date date) 

Method Source Code

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

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

public class Main {
    /**/*from   ww w  .  j  a  va2 s  .  c o  m*/
     * Method dateToString.
     * @param date Date
     * @return String
     */
    public static String dateToString(Date date) {
        Calendar expireCa = new GregorianCalendar();
        // Use the incoming Date object to set the Year, Month, and Day
        expireCa.setTime(date);

        int year = expireCa.get(Calendar.YEAR);
        int month = expireCa.get(Calendar.MONTH);
        int day = expireCa.get(Calendar.DAY_OF_MONTH);

        StringBuffer sb = new StringBuffer();
        // I think we can trust we are working with 4 digit years
        sb.append(year);
        // Increment the month due to Jan = 0 with Calendar.MONTH
        month++;
        // Zeropad Month if needed
        if (month < 10) {
            sb.append('0');
        }
        sb.append(month);
        // Zeropad Day if needed
        if (day < 10) {
            sb.append('0');
        }
        sb.append(day);
        return sb.toString();
    }
}

Related

  1. dateToNumber(Date date)
  2. dateToQTime(final Date date)
  3. dateToRRNDate(Date date)
  4. DateToStr(java.util.Date val)
  5. dateToString(Date date)
  6. dateToString(Date date)
  7. dateToString(final Date date)
  8. dateToUnixTimestamp(Date date)
  9. dateToUTC(Date d)