The Artima Developer Community
Sponsored Link

Java Answers Forum
Trying to write a date program

6 replies on 1 page. Most recent reply: Aug 5, 2003 12:36 AM by Vincent O'Sullivan

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 6 replies on 1 page
J Stevens

Posts: 7
Nickname: js
Registered: Jul, 2003

Trying to write a date program Posted: Jul 31, 2003 3:44 PM
Reply to this message Reply
Advertisement
I do not know enough about java and wanted to know how to write a date program that uses the java.util.Date class. I tried, but cannot seem to get it to work. Would someone be will to give me any tips or help?

I was thinking along these lines ...

import java.util.*;
import java.util.Date;
 
 
class DateProgram
{
 
private static final String DATE = now();
  private static final String now()
  {
    try {
        return new SimpleDateFormat(dateFormat).format("");
    } catch (Throwable t) {
        return new Date();
    }
  }
}
 
 
/*
Error messages
\DateProgram.java:14: cannot resolve symbol
symbol  : class SimpleDateFormat
location: class Test
        return new SimpleDateFormat(dateFormat).format("");
                   ^
\DateProgram.java:14: cannot resolve symbol
symbol  : variable dateFormat
location: class Test
        return new SimpleDateFormat(dateFormat).format("");
                                    ^
\DateProgram.java:16: incompatible types
found   : java.util.Date
required: java.lang.String
        return new Date();
               ^
3 errors
*/




Thanks in advance


David

Posts: 18
Nickname: malven
Registered: Jul, 2003

Re: Trying to write a date program Posted: Jul 31, 2003 4:10 PM
Reply to this message Reply
I'm not sure what you want your program to do, but as for the compiler error messages you're getting:

1. You need to import java.text.SimpleDateFormat.

2. Looks like you are referencing a data member named "dateFormat" yet there is no such member in the code. So you either need to create a SimpleDateFormat initialized with the specific format you want, or use one of the factory methods in java.text.DateFormat, e.g.

private DateFormat dateFormat =
DataFormat.getDateInstance();

3. In the catch block you are returning an object of type java.util.Date, yet your method is declared to return a String.

I'm not sure what you want the now() method to do. The argument to SimpleDateFormat.format() must be a Date object, not a string. If you just want now() to return a string represntation of the current date then this should be sufficient:

private static String now() {
return dateFormat.format(new Date());
}

J Stevens

Posts: 7
Nickname: js
Registered: Jul, 2003

Re: Trying to write a date program Posted: Jul 31, 2003 6:11 PM
Reply to this message Reply
Thanks David:

I was lost. I really have to read more (know any good books)
I wanted to just see if java would print the date, and using the java.util class made it simple and fast:

import java.util.Date;  // for class Date
import java.util.GregorianCalendar; // ** Alternative since java.util.Date is being depreciated
 
public class TimeDisplay
{
 
	public static void main (String [] args)
	{
 
 
		Date d = new Date() ;
		System.out.println("Todays date is :" + d.toString()  );
		System.out.println("Todays time is :" + d.getTime()  );
 
	}
}



**The java.util.Date Class
Many of the methods in java.util.Date have been deprecated in favor of other APIs that better support internationalization. The following table provides a list of these methods and their alternatives.

http://java.sun.com/docs/books/tutorial/post1.0/converting/deprecated.html

Vincent O'Sullivan

Posts: 724
Nickname: vincent
Registered: Nov, 2002

Re: Trying to write a date program Posted: Aug 1, 2003 1:19 AM
Reply to this message Reply
When I converted from VB to Java, pretty much the first thing I did was to try to write a 'simple' program that displayed and refreshed the date and time.

It's something that would take less than 5 minutes and a couple of lines of code in VB. In java, I reckon it took me about a week and a half, due to having to get to grip with dates and calendars and Gregorian calendars and AWT v. Swing and applets v. applications and creating my own timer for the refresh, etc, etc. The resulting app looked bad, too. It was an experience that has coloured my opinion of the language ever since.

Vince.

David

Posts: 150
Nickname: archangel
Registered: Jul, 2003

Re: Trying to write a date program Posted: Aug 1, 2003 6:11 AM
Reply to this message Reply
It certainly is one of the nastier APIs of Java.

Charles Bell

Posts: 519
Nickname: charles
Registered: Feb, 2002

Re: Trying to write a date program Posted: Aug 3, 2003 12:26 PM
Reply to this message Reply
The following is an example of using SimpleDateFormat
/* DateDifference.java
 * June 21, 2003
 */
 
import java.text.*;
import java.util.*;
 
/** How to determine the difference between two dates formatted as follows:
 *    6/16/2003 06:30:35
 *    6/16/2003 06:31:37

 */
public class DateDifference{
 
    
    public static void main(String[] args){
        new DateDifference();
    }
    
    public void test(){
        String first = "6/16/2003 06:30:35";
        String second = "6/16/2003 06:31:37";
        Date firstDate = getDate(first);
        Date secondDate = getDate(second);
        System.out.println("firstDate: " + firstDate.toString());    
        System.out.println("secondDate: " + secondDate.toString());    
        System.out.println("Difference in milliseconds: " + getDifference(first, second));    
    
    }
    
    /** Returns the difference in milliseconds between two dates formatted as:
     *    6/16/2003 06:30:35
     *    6/16/2003 06:31:37
     */
    public long getDifference(String first, String second){
        SimpleDateFormat format = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss");
        Date firstDate = format.parse(first, new ParsePosition(0));
        Date secondDate = format.parse(second, new ParsePosition(0));
        return Math.abs(firstDate.getTime() - secondDate.getTime());
        
    }
    
    /** Returns a java.util.Date from a string formatted as:
     *    6/16/2003 06:30:35
     */
    public Date getDate(String dateString){
        SimpleDateFormat format = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss");
        return format.parse(dateString, new ParsePosition(0));
    }
}

Vincent O'Sullivan

Posts: 724
Nickname: vincent
Registered: Nov, 2002

Re: Trying to write a date program Posted: Aug 5, 2003 12:36 AM
Reply to this message Reply
> The following is an example of using SimpleDateFormat...

Does the moniker SimpleDateFormat imply the existence of a ComplicatedDateFormat class?

Flat View: This topic has 6 replies on 1 page
Topic: Got this null pointer exception problem Previous Topic   Next Topic Topic: how to use timer?

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use