1 Replies - 4644 Views - Last Post: 30 October 2006 - 01:47 PM Rate Topic: -----

#1 commlink  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 19-October 06

Comparing String with anotherString - Dates

Posted 30 October 2006 - 01:29 PM

Hi Everyone... so I am back for more help!!!!

So my goal this time is to compare the varibale USDCHF_DATE who's value is 2005/06/06 17:00 with the variable formatedDATE who's value is 2006/10/30 15:27

CODE HERE:
import com.javaparts.dde.*;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.text.ParseException;
import java.util.Date;


public class CompareDDETime {
	public static void main(String[] args) {
		DDEClient client;
		Conversation sheet1 = null;
		client = DDEClient.getInstance();
		 // Make a new Date object. It will be initialized to the current time.
		Date now = new Date();

		SimpleDateFormat format =
			new SimpleDateFormat("yyyy'/'MM'/'dd HH:mm");
		   String formatedDATE = format.format(now);
		   System.out.println("Current time is " + formatedDATE);
	  try {

			sheet1 = client.connect("Excel", "Sheet1");

			System.out.println("Connected to Excel Sheet1");
			System.out.println("");
			String USDCHF_DATE = new String(sheet1.request("R3C7"));
			String GBPUSD_DATE = new String(sheet1.request("R4C7"));
			String USDJPY_DATE = new String(sheet1.request("R5C7"));
			String EURUSD_DATE = new String(sheet1.request("R6C7"));
			System.out.println("The last USDCHF modification date from the DDE Server was at " + USDCHF_DATE);
			System.out.println("The last GBPUSD modification date from the DDE Server was at " + GBPUSD_DATE);
			System.out.println("The last USDJPY modification date from the DDE Server was at " + USDJPY_DATE);
			System.out.println("The last EURUSD modification date from the DDE Server was at " + EURUSD_DATE);

//			   if (EURUSD_DATE < formatedDATE) {
//		 System.err.println("All is well!");
//	 } else {
//		  System.err.println("Problem deteced!");
//	 }


		} catch(DDEException e) {
			System.out.println(e);
		} finally {
			// disconnect in a finally clause
			try {sheet1.close();} catch (Exception e) {}
		}

		client.close();
	}
}



The Scripts output is like so:


C:\Program Files\DDEForJava\examples\com\javaparts\dde\examples>java CompareDDET
ime

Connected to Excel Sheet1

The last USDCHF modification date from the DDE Server was at 2005/06/06 17:00

The last GBPUSD modification date from the DDE Server was at 2005/06/06 17:00

The last USDJPY modification date from the DDE Server was at 2005/06/06 17:00

The last EURUSD modification date from the DDE Server was at 2005/06/06 17:00

Is This A Good Question/Topic? 0
  • +

Replies To: Comparing String with anotherString - Dates

#2 horace  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 290
  • View blog
  • Posts: 1,898
  • Joined: 25-October 06

Re: Comparing String with anotherString - Dates

Posted 30 October 2006 - 01:47 PM

use compareTo() method in Date, see URL
http://java.sun.com/.../util/Date.html

simple example using your dates 2006/10/30 15:27 and 2005/06/06 17:00 and compareTo() from Date

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.text.*;
import java.util.Date;

public class CompareTime {
	public static void main(String[] args) {
		 // Make a new Date object. It will be initialized to the current time.
		Date now = new Date();
		int compare =0;
		SimpleDateFormat format =
			new SimpleDateFormat("yyyy'/'MM'/'dd HH:mm");
		   String formatedDATE = format.format(now);
		   System.out.println(formatedDATE);
		   // create a couple of dates using the specified format
		   Date date1=format.parse("2006/10/30 15:27",new ParsePosition(0));
		   System.out.println("date1 = " + date1);
		   Date date2=format.parse("2005/06/06 17:00",new ParsePosition(0));
		   System.out.println("date2 = " + date2);
		   // compare them
		   if(date1.compareTo(date2) == 0)
			  System.out.println(date1 + " equals " + date2);
		   else
			   if(date1.compareTo(date2) < 0)
				  System.out.println(date1 + " < " + date2);
			   else
				  System.out.println(date1 + " > " + date2);
		   // set a new date and compare again
		   date2=format.parse("2007/06/06 17:00",new ParsePosition(0));
		   System.out.println("date2 = " + date2);
		   if(date1.compareTo(date2) == 0)
			  System.out.println(date1 + " equals " + date2);
		   else
			   if(date1.compareTo(date2) < 0)
				  System.out.println(date1 + " < " + date2);
			   else
				  System.out.println(date1 + " > " + date2);
	}
}



a program run gives
date1 = Mon Oct 30 15:27:00 GMT 2006
date2 = Mon Jun 06 17:00:00 BST 2005
Mon Oct 30 15:27:00 GMT 2006 > Mon Jun 06 17:00:00 BST 2005
date2 = Wed Jun 06 17:00:00 BST 2007
Mon Oct 30 15:27:00 GMT 2006 < Wed Jun 06 17:00:00 BST 2007

This post has been edited by horace: 31 October 2006 - 01:00 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1