[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

How do I cast a string to a date object

Posted on 2001-08-14
3
Medium Priority
?
226 Views
Last Modified: 2010-03-31
Hello, I'm recieving a date as a string through my main method.  How do I cast this string to a date object to be compared to another date object in the class?

Thanks,
0
Comment
Question by:gshoates
3 Comments
 
LVL 6

Accepted Solution

by:
dorothy2 earned 400 total points
ID: 6384847
I wrote a CustomDate class which handled conversion between Strings and Dates. Here is the code for returning dates from strings, and strings from dates. I think the setDate method throws the parsing error if the String parameter is something other than a date.

Dorothy

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



   public static String getDate(java.util.Date d, String type) {
    SimpleDateFormat formatter = null;
    type = type.toUpperCase();
    try {
      if("L".equals(type)) {
        formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        if(DEBUG_ON) System.out.println("Long chosen.");
      }
      else if("S".equals(type)) {
        formatter = new SimpleDateFormat("MM/dd/yyyy");
        if(DEBUG_ON) System.out.println("Short chosen.");
      }
      return(formatter.format(d));
    } catch(Exception ex) {
      ex.printStackTrace();
    }

  public static java.util.Date setDate(String s, String type) {
    SimpleDateFormat formatter = null;
    type = type.toUpperCase();
    try {
      if("L".equals(type)) {
        formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
      }
      else if("S".equals(type)) {
        formatter = new SimpleDateFormat("MM/dd/yyyy");
      }
      return(formatter.parse(s));
    } catch(Exception pe) { ex.printStackTrace(); }
  }
0
 

Author Comment

by:gshoates
ID: 6385275
Thanks
0
 
LVL 92

Expert Comment

by:objects
ID: 6387204
DateFormat also has some static methods for returning various 'standard' date/time formats.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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 …
In this post we will learn different types of Android Layout and some basics of an Android App.
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 tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses
Course of the Month18 days, 10 hours left to enroll

834 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