File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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 EJB 3 in Action this week in the EJB and other Java EE Technologies 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: 36478
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: 19541

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
I agree. Here's the link:
subject: BigDecimal to Date Conversion
Similar Threads
a question about the ActionForm 's property type
BigDecimal to Date
Date comparision in java
data type for large decimals
Loosing Object-Oriented Design points - URLyBird