In this example we are going to convert String into
In java date conversion, two packages are used .They are java.util.* and java.text.*. The java.util.* package provides three subclasses that are named as java.util.Date, java.util.Time and java.util.Timestamp class.
The java.util.Date class represents a specific instant in time. The time is with millisecond precision. In JDK 1.1, the java.util.Date class has two additional functions. It allows the interpretation of date in year, month, day, hour, minute, and second values. It also allows the formatting and parsing of date strings. The Calendar class of JDK 1.1 is used to convert between dates and time fields and the DateFormat class is used to format and parse date strings. The Date class supports the universal time (UTC). That means 1 day = 24 × 60 × 60 = 86400 seconds in all cases.
All methods of Date either accept or return values in year, month, day, hour, minute, and second.
The following representations are used:
1. y is used to represent an year which is an integer value. The value will be y-19000.
2. m is used to represent a month which has values form 0 to 11. In which 0 is used for January, 1 is used for February and so on, thus 11 is used for December.
3. A date (day of month) is represented by an integer from 1 to 31 in the usual manner.
4. An hour is represented by an integer from 0 to 23. Thus, the hour from midnight to 1 a.m. is hour 0, and the hour from noon to 1 p.m. is hour 12.
5. A minute is represented by an integer from 0 to 59 in the usual manner.
6. A second is represented by an integer from 0 to 61, the values 60 and 61 occur only for leap seconds and even then only Java provides implementation that actually track leap seconds correctly.
The Constructor of Date:
Date(): This constructor is used to allocate a Date object and initializes it. The object used to represent the time and date is measured in nearest milliseconds. The following constructors has been deprecated and are replaced by Calendar.set(year + 1900, month, date) / GregorianCalendar(year + 1900, month, date), Calendar.set(year + 1900, month, date, hrs, min) / GregorianCalendar(year + 1900, month, date, hrs, min) and Calendar.set(year + 1900, month, date, hrs, min, sec) / GregorianCalendar(year + 1900, month, date, hrs, min, sec) respectively.
Date(int year, int month, int date)
Date(int year, int month, int date, int hrs, int min)
Date(int year, int month, int date, int hrs, int min, int sec)
Date(long date): This is used to allocate a Date object and initializes it to represent the specified number of milliseconds since the standard base time known as "the epoch", namely January 1, 1970, 00:00:00 GMT.
Date(String s): This is deprecated. It is replaced by DateFormat.parse(String s).
The java.text.* pacakge is used for formatting and parsing of dates. It allows formatting of text from date , parsing from date to date and normalization. The java.text.DatefFormat class is the super class of java.text.SimpleDateFormat class. The java.text.SimpleDateFormat is a concrete class for formatting and parsing of dates. The java.text.SimpleDateFormat class allows us to start the user defined patterns for date-time formatting. We can encourage to anyone to create a date-time formatter with either getTimeInstance, getDateInstance, or getDateTimeInstance in DateFormat. The methods within the class returns a date/time.formatter with a default format pattern.
To specify the time format using a time pattern string all ASCII letters are reserved as pattern letters, which are defined as the following:
|M||month in year||(Text & Number)||July & 07|
|d||day in month||(Number)||10|
|h||hour in am/pm (1~12)||(Number)||12|
|H||hour in day 0~23)||( (Number)||1|
|m||minute in hour||(Number)||30|
|s||second in minute||(Number)||55|
|E||day in week||(Text)||Tuesday|
|D||day in year||(Number)||189|
|F||day of week in month||(Number)||2 (2nd Wed in July)|
|w||week in year||(Number)||27|
|W||week in month||(Number)||2|
||hour in day (1~24)||(Number)||24|
|K||hour in am/pm (0~11)||(Number)||0|
|z||time zone||(Text)||Pacific Standard Time|
In this example we are going to convert string into date. We are creating
DateFormat by using SimpleDateFormat then parse the string with the date format. We are creating an object of Date to store the parsed formatted
The method used:
This method is used to parse date/time string into date. The return type of this method is Date. This method throws ParseExceptionParse Exception if the given string cannot be parsed as a date.
The code of the program is given below:
The output of the program is given below:
C:\date>javac StringToDate.java C:\date>java StringToDate Today is Mon Jun 11 00:00:00 GMT+05:30 2007
Recommend the tutorial