This week's book giveaway is in the OCMJEA forum.
We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes BigDecimal to Date Conversion Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "BigDecimal to Date Conversion" Watch "BigDecimal to Date Conversion" New topic

BigDecimal to Date Conversion

Sanjeev Dubey

Joined: Aug 07, 2008
Posts: 16
I want to convert a a date in BigDecimal to Date

BigDecimal date = new BigDecimal(20080221);

Date date= 21-Feb-08 ( with DD-MMM-YY format)

Can any one help?
Sanjeev Dubey

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.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

Joined: Oct 13, 2005
Posts: 38513
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

Joined: Oct 27, 2005
Posts: 19684

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.

How To Ask Questions How To Answer Questions
Don't get me started about those stupid light bulbs.
subject: BigDecimal to Date Conversion