Here you can find the source of dateToString(Date date)
Parameter | Description |
---|---|
date | Date |
public static String dateToString(Date date)
//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(); } }