Java Tutorials
How to Convert Date to String
How to convert date to string

Maybe it is strange but... for some people - I think there are a lot of them - it is difficult to convert date to string in Java, especially for beginners. There are a lot of classes and functions in Java that work with Date variables. There are most usual of them here:
Date
Calendar
GregorianCalendar
DateFormat
TimeZone
We combined only some of them to create simple class for converting date to string.
The method dateToString(Date dtK,String sFormat) of class DateTimeBean converts Date variable to String according to rules that are very close to them in PHP. This function is very good to modify it.
The second method of the class makes back conversion.
package datetime;

import java.util.*;

public class DateTimeBean
{
/*f*********************
Converts Date to String
<dtK> - date
<sFormat> - format-string

Format character
Y                 A full numeric representation of a year, 4 digits,
examples: 1999 or 2003 m Numeric representation of a month, with leading zeros, 01
through 12 d Day of the month, 2 digits with leading zeros, 01 to 31 H 24-hour format of an hour with leading zeros, 00 through 23 i Minutes with leading zeros, 00 to 59 s Seconds, with leading zeros, 00 through 59 Z Seconds, with leading zeros 000 through 999 ***********************/ public String dateToString(Date dtK,String sFormat) { String sDate; int nYear,nMonth,nDay,nHour,nMinute,nSecond,nMS; Calendar clnK; String sf; int jc; clnK=Calendar.getInstance(Locale.US); clnK.setTime(dtK); nYear=clnK.get(Calendar.YEAR); nMonth=1+clnK.get(Calendar.MONTH); nDay=clnK.get(Calendar.DAY_OF_MONTH); nHour=clnK.get(Calendar.HOUR_OF_DAY); nMinute=clnK.get(Calendar.MINUTE); nSecond=clnK.get(Calendar.SECOND); nMS=clnK.get(Calendar.MILLISECOND); sDate=""; for(jc=0;jc<sFormat.length();jc++) { switch(sFormat.charAt(jc)) { case 'Y': sDate+=nYear; break; case 'm': sf=""+nMonth;if(nMonth<10)sf="0"+sf; sDate+=sf; break; case 'd': sf=""+nDay;if(nDay<10)sf="0"+sf; sDate+=sf; break; case 'H': sf=""+nHour;if(nHour<10)sf="0"+sf; sDate+=sf; break; case 'i': sf=""+nMinute;if(nMinute<10)sf="0"+sf; sDate+=sf; break; case 's': sf=""+nSecond;if(nSecond<10)sf="0"+sf; sDate+=sf; break; case 'Z': sf=""+nMS;if(nMS<10)sf="0"+sf; sDate+=sf; break; default: sDate+=sFormat.substring(jc,jc+1); } } return sDate; } /*f********************* Converts String to Date <sDate> - string with date like "YYYY-MM-DD HH:NN:SS.ZZZ" ***********************/ public Date stringToDate(String sDate) { Date dtRes; Calendar clnK; int nYear,nMonth,nDay,nHour,nMinute,nSecond,nMS; String sf; for(;sDate.length()<23;) sDate+="0"; sf=sDate.substring(0,4); nYear=Integer.parseInt(sf); sf=sDate.substring(5,7); nMonth=Integer.parseInt(sf)-1; sf=sDate.substring(8,10); nDay=Integer.parseInt(sf); sf=sDate.substring(11,13); nHour=Integer.parseInt(sf); sf=sDate.substring(14,16); nMinute=Integer.parseInt(sf); sf=sDate.substring(17,19); nSecond=Integer.parseInt(sf); sf=sDate.substring(20,23); nMS=Integer.parseInt(sf); clnK=Calendar.getInstance(Locale.US); clnK.set(nYear,nMonth,nDay,nHour,nMinute,nSecond); clnK.set(Calendar.MILLISECOND,nMS); dtRes=new Date(); dtRes=clnK.getTime(); // sf=dateToString(dtRes,"Y-m-d H:i:s.Z"); return dtRes; } }
Example of using this function.
DateTimeBean ODateTimeBean=new DateTimeBean();
Date dtNow=new Date();
String sFormat="Y-m-d H:i:s.Z";
sRes=ODateTimeBean.dateToString(dtNow,sFormat);
//Will return something like "2004-03-24 08:24:17.390"

ZIP with demostration for JBuilder 6 is here: XMLLoadUse.zip.
Remarks
zehra maria
2010-07-21 15:56:05
http://youtube.com
ı m a going to what do you lıke ı l ıke
Okpali d great
2008-12-28 03:03:04
hi
how can i convert calendar object to date and vice versa?
answers would aid me thanks Okpali D great
King
2008-02-20 07:18:08
can i ask one thing? if i want to show the date object which all the value ( year, date, month, time..) is zeros. how did i have to do?
REMA
2007-09-17 08:05:56
This is very useful for me.

Thanks a lot for all...
Vinod
2007-07-27 19:13:23
WOW thts a lot of hard work!!!
But try this
package com.jpmchase.pag.amalgam.common.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Test {
public static void main(String [] args) throws ParseException
{
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMM");
Date d= sdf.parse("200712");

SimpleDateFormat nsdf = new SimpleDateFormat("dd-MM-yyyy");
System.out.print(nsdf.format(d));
}
}
Karim
2006-08-03 13:04:34
kelyounsi@ilog.co.uk
hi guys,
There's a better way using the \"SimpleDateFormat\" from \"java.text\"
Sajeev
2006-07-11 21:30:44
sajeev_m_e@yahoo.com
Excellent That was a one stopper . Please do keep posting such usefult Contents. thanks Any way
ddd
2006-05-14 05:04:32
ddddd@hotmail.com
sssssss
kumar
2006-04-11 02:31:17
i need more example for date formatter and sample program that should be good
suresh
2006-04-11 02:30:01
about date formatter more detailed
Add Your Remark:
Your Name*:
Email:
Hide Email    Subscribe to Site News
Web Site:
Message*:
Other tutorials:
Flash Tutorials
JavaScript Tutorials
PHP Tutorials
General Questions