View Full Version : Converting a BigDecimal into a date
Guest.Visitor
01-01-1995, 02:00 AM
I am using BigDecimal data type to return an 8P 0 from an AS/400 RPG program to a java program. The number is a date, and I get it in my Java program fine, but I need to format it with slashes for example 08302000 --> 08/30/2000 I looked in the toolbox programmer guide for a method, but no luck. I also tried to cast the BigDecimal did not work. sample of my code: BigDecimal decimalValue2 = new BigDecimal(0); AS400PackedDecimal zoneConv2 = new AS400PackedDecimal(8, 0); byte zonedInput2 = zoneConv2.toBytes(decimalValue2); parmList2 = new ProgramParameter(zonedInput, 8); BigDecimal zonedReturn = (BigDecimal)new AS400PackedDecimal(8, 0).toObject( parmList2.getOutputData() ); pw.println("<TD COLSPAN=9 align right bgcolor #FFFFFF>" + zonedReturn.toString() + "</TD>"); Thanks in advance Tom
J.Pluta
08-24-2000, 12:09 PM
Formatting dates is not part of the toolbox; it's something you do in Java. In order to support internationalization, including every type of calendar known to man, they did some very complex things. However, the following code may help. It's a test program that converts a YYYYMMDD field to a date, then formats it and prints it. It also shows off some of the more interesting capabilities of the SimpleDateFormat. <pre>import java.util.*; import java.text.*; public class DateTest1 { public static void main(String args) { int dateValue = 20010731; // Convert YYYYMMDD date to year, month and day int year = dateValue / 10000; int month = (dateValue / 100) % 100; int day = dateValue % 100; // Convert to date (month is ZERO-based!) Date myDate = new GregorianCalendar(year, month-1, day).getTime(); // Format the date and print it SimpleDateFormat formatter = new SimpleDateFormat("MM/dd/yyyy"); String dateString = formatter.format(myDate); System.out.println(dateString); // Format the date for our European friends formatter = new SimpleDateFormat("dd-MMM-yy"); dateString = formatter.format(myDate); System.out.println(dateString); // Format the date for business letterhead formatter = new SimpleDateFormat ("EEEE, MMMM dd, yyyy"); dateString = formatter.format(myDate); System.out.println(dateString); // Exit System.exit(0); } }</pre>Hope this helps a little. I realize it's not particularly easy to read, but it should work and should give you some idea of how powerful the date formatting is. A Date object also has time embedded in it, so you can edit and display hours, minutes and seconds. Joe P.S. Anybody interested in the results, they are as follows: 07/31/2001 31-Jul-01 Tuesday, July 31, 2001
Guest.Visitor
08-24-2000, 12:10 PM
You cannot format a numeric value with slashes. It must first be converted to String and then insert the slashes. Dan
Powered by vBulletin® Version 4.1.5 Copyright © 2013 vBulletin Solutions, Inc. All rights reserved.