| Author |
BigDecimal to Date Conversion
|
Sanjeev Dubey
Greenhorn
Joined: Aug 07, 2008
Posts: 16
|
|
I want to convert a a date in BigDecimal to Date
BigDecimal date = new BigDecimal(20080221);
to
Date date= 21-Feb-08 ( with DD-MMM-YY format)
Can any one help?
|
 |
Sanjeev Dubey
Greenhorn
Joined: Aug 07, 2008
Posts: 16
|
|
public static java.sql.Date dateToDB(BigDecimal dateFrom){
if ( dateFrom == null )
return null;
String dateStr = dateFrom.toString();
String year = dateStr.substring(0,4);
String month = dateStr.substring(4,6);
String day = dateStr.substring(6,8);
calendar.set(Calendar.YEAR, Integer.parseInt(year));
calendar.set(Calendar.MONTH,Integer.parseInt(month)-1);
calendar.set(Calendar.DAY_OF_MONTH,Integer.parseInt(day));
calendar.set(Calendar.HOUR, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);
calendar.set(Calendar.AM_PM, Calendar.AM);
java.sql.Date date = new java.sql.Date(calendar.getTimeInMillis());
return date;
}
got the solution....thanks ..if any one already started thinking about it
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32833
|
|
Far better to use the mod and divide operations.
And why would you want an integer in a BigDecimal in the first place?
|
 |
Rob Spoor
Sheriff
Joined: Oct 27, 2005
Posts: 19232
|
|
The most robust solution is to use the existing library:
Keep in mind though, that a Date object has no real formatting. It represents a moment in time. Sure you see a date when you call its toString() method, but that's not the Date's actual format - it has none. Use (Simple)DateFormat to convert it into a formatted string.
|
SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
|
 |
 |
|
|
subject: BigDecimal to Date Conversion
|
|
|