?
Solved

Date problem, part 2

Posted on 2002-04-09
4
Medium Priority
?
224 Views
Last Modified: 2011-09-20
I am passing in 3 dates from an HTML page:

<PARAM NAME = "date" Value="04/04/2002 01:00:00AM,04/04/2002 23:00:00PM,04/05/2002 01:00:00AM">

The difference is 22 hours and 2 hours, however when i try to convert it based on the code below, I get

0 0.0
1 13.0
2 720.0

It seems like if they are on the same day it works fine, but when you go to the next day, it should calculate 24.0, not 720?   I try 04/05/2002 03:00:00AM and it just adds 2 on it.

import java.awt.*;
import java.applet.Applet;
import java.util.*;
import java.lang.*;
import java.text.*;

public class graph extends Applet
{
     public void paint(Graphics g)
     {
           int Y1 = 10;
           int Y2 = 350;
           int X1 = 50;
           int X2 = 550;
           int ySpan = 8;
           
          String dates = getParameter("date");
          String data = getParameter("data");
         
          java.text.SimpleDateFormat date_format = new java.text.SimpleDateFormat("dd/MM/yyyy hh:mm:ssa");
         
          StringTokenizer aryData = new StringTokenizer(data,",");
          StringTokenizer aryDates = new StringTokenizer(dates,",");
          int numitems = aryData.countTokens();
         
          String[] arydates = new String[numitems];
          long[] aryDTdiff = new long[numitems];
         
          DateFormat df = DateFormat.getDateInstance();
          for (int i=0;i<numitems;i++)
          {
               arydates[i] = aryDates.nextToken();
               try{aryDTdiff[i] = date_format.parse(arydates[i]).getTime();}
               catch(java.text.ParseException e){};
          }
         
          //initalize them
          float lowVal = Float.valueOf(aryData.nextToken()).floatValue();
          float highVal = lowVal;
          for (int i=1;i<numitems;i++)
          {
             float tempVal = Float.valueOf((aryData.nextToken())).floatValue();
             
             if (tempVal > highVal) highVal = tempVal;
             if (tempVal < lowVal) lowVal = tempVal;
           
          }
         
          //cycle through the Y Coordinate values
          g.drawString(Float.toString(highVal+1.0f),X1-35,Y1+5);
          g.drawLine(X1-2,Y1,X1+5,Y1);
         
          NumberFormat nf = NumberFormat.getNumberInstance();
               
          for (int i=1; i<=ySpan;i++)
          {
               double d = (highVal - (highVal-lowVal)/ySpan) - (highVal-lowVal)/ySpan * i;
               nf.setMaximumFractionDigits(2);
               d = new Double(nf.format(d)).doubleValue();
               float f = (float)d;
               g.drawLine(X1-2,(Y2-Y1)/ySpan*i,X1+5,(Y2-Y1)/ySpan*i);
                g.drawString(Float.toString(f),X1-35,(Y2-Y1)/ySpan*i + 5);
          }
         
          for (int q=(numitems-1); q >= 0; q--)
          {
               g.drawString(Integer.toString(q) + "  " + Float.toString((aryDTdiff[q]-aryDTdiff[0])/1000/60/60),X1+200,Y1*(q+20));
               //g.drawString(Integer.toString(q) + "  " + date_format.parse(arydates[q]).getTime(),X1+200,Y1*(q+20));
               //g.drawString(Integer.toString(q) + "  44",X1+200,Y1*(q+20));
         
          }
                                         
         
          //draw graph layout thingy
          g.setColor(Color.black);
          g.drawLine(X1,Y1,X1,Y2);
          g.drawLine(X1,Y2,X2,Y2);
         
     } // end paint
}// end graph Applet
0
Comment
Question by:thunderchicken
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 11

Author Comment

by:thunderchicken
ID: 6929322
Sorry, wrong Params

<PARAM NAME = "date" Value="04/04/2002 01:00:00AM,04/04/2002 2:00:00PM,04/05/2002 01:00:00AM">
0
 
LVL 92

Accepted Solution

by:
objects earned 400 total points
ID: 6929588
It's not the next day, it's the next month.
4 April -> 4 May.
Resulting in 720 hours.

Change your DateFormat to: "MM/dd/yyyy hh:mm:ssa"
0
 
LVL 11

Author Comment

by:thunderchicken
ID: 6929596
Jesus, I'm stupid.

Thanks
0
 
LVL 92

Expert Comment

by:objects
ID: 6929625
No worries :-)

http://www.objects.com.au
Brainbench MVP for Java 1
http://www.brainbench.com
0

Featured Post

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Suggested Courses
Course of the Month15 days, 6 hours left to enroll

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question