Solved

Date question

Posted on 2002-04-09
3
198 Views
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


---|-------------------------------------|----|




0
Comment
Question by:thunderchicken
  • 2
3 Comments
 
LVL 9

Accepted Solution

by:
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
Arrays.sort(datearray);
// 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;
System.out.println();
for (int i = 0; i < 3; i++) {
    for (int j=0; j < datearray[i]; j++)
        System.out.print("-");
    System.out.print("|");
}
System.out.println();
0
 
LVL 11

Author Comment

by:thunderchicken
ID: 6928746
???

C:\inetpub\wwwroot\graph.java:34: 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
          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);
         
               
          }
         
         
          //draw graph layout thingy
          g.setColor(Color.black);
          g.drawLine(X1,Y1,X1,Y2);
          g.drawLine(X1,Y2,X2,Y2);
         
         
          float[] aryItems = new float[numitems];
         
               
     } // end paint
}// end graph Applet
0
 
LVL 11

Author Comment

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

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

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…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This video teaches viewers about errors in exception handling.

856 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