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
Solved

How do I cast a string to a date object

Posted on 2001-08-14
3
220 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 100 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: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

Suggested Solutions

Title # Comments Views Activity
Weekend adv creator 3 52
jar file executable 12 58
Unhandled exception type Exception 18 42
Desingning Refactoring existing code 2 26
For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This video teaches viewers about errors in exception handling.

860 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