[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

How do I cast a string to a date object

Posted on 2001-08-14
3
Medium Priority
?
225 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
[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
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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

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 Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses

650 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