Date question

Posted on 2002-04-09
Last Modified: 2010-03-31
I'm about to do this process... blah

I am passing in a string of dates and displaying it in a java applet.

Say I have a line graph, I want to display the dates in order from X1 to X2 and they need to be spaced depending on the difference of the date...

for example

dates[0] = "04/04/2002 5:00:00AM"
dates[1] = "04/04/2002 2:00:00PM"
dates[2] = "04/04/2002 4:00:00PM"

should display something like


Question by:thunderchicken
  • 2

Accepted Solution

Venci75 earned 200 total points
ID: 6928508
try this:

// the date format
java.text.SimpleDateFormat date_format = new java.text.SimpleDateFormat("dd/MM/yyyy h:mm:ssa");
// initializing the date strings
String[] dates = new String[3];
dates[0] = "04/04/2002 5:00:00AM";
dates[1] = "04/04/2002 2:00:00PM";
dates[2] = "04/04/2002 4:00:00PM";

// getting the dates as longs
long[] datearray = new long[3];
datearray[0] = date_format.parse(dates[0]).getTime();
datearray[1] = date_format.parse(dates[1]).getTime();
datearray[2] = date_format.parse(dates[2]).getTime();
// sorting
// getting the difference
datearray[2] = datearray[2] - datearray[1];
datearray[1] = datearray[1] - datearray[0];
datearray[0] = 0;
// rounding to hours
datearray[1] /= 1000 * 60 * 60;
datearray[2] /= 1000 * 60 * 60;
for (int i = 0; i < 3; i++) {
    for (int j=0; j < datearray[i]; j++)
LVL 11

Author Comment

ID: 6928746

C:\inetpub\wwwroot\ Exception java.text.ParseException must be cau
ght, or it must be declared in the throws clause of this method.

aryDTdiff[i] = date_format.parse(arydates[i]).getTime();


import java.awt.*;
import java.applet.Applet;
import java.lang.Object;
import java.lang.String;
import java.util.StringTokenizer;
import java.lang.*;
import java.lang.Math;
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 h: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];
          for (int i=0;i<numitems;i++)
               arydates[i] = aryDates.nextToken();
               aryDTdiff[i] = date_format.parse(arydates[i]).getTime();
          //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
          NumberFormat nf = NumberFormat.getNumberInstance();
          for (int i=1; i<=ySpan;i++)
               double d = (highVal - (highVal-lowVal)/ySpan) - (highVal-lowVal)/ySpan * i;
               d = new Double(nf.format(d)).doubleValue();
               float f = (float)d;
                g.drawString(Float.toString(f),X1-35,(Y2-Y1)/ySpan*i + 5);
          //draw graph layout thingy
          float[] aryItems = new float[numitems];
     } // end paint
}// end graph Applet
LVL 11

Author Comment

ID: 6928824
Thanks, i figured out the try/catch thingy.  I'll be asking more questions later...

Featured Post

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

730 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