Solved

Date question

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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…

705 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now