• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Bear Bibeault
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Jj Roberts
  • Tim Holloway
  • Piet Souris
Bartenders:
  • Himai Minh
  • Carey Brown
  • salvin francis

BigDecimal to Date Conversion

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Marshal
Posts: 71055
291
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Far better to use the mod and divide operations.

And why would you want an integer in a BigDecimal in the first place?
 
Sheriff
Posts: 22041
113
Eclipse IDE Spring VI Editor Chrome Java Ubuntu Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
reply
    Bookmark Topic Watch Topic
  • New Topic