Solved

Date question

Posted on 2002-04-09
3
200 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
[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
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

What Is Transaction Monitoring and who needs it?

Synthetic Transaction Monitoring that you need for the day to day, which ensures your business website keeps running optimally, and that there is no downtime to impact your customer experience.

Question has a verified solution.

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

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

734 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