Solved

Date question

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

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

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 …
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses

624 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