Solved

How do I cast a string to a date object

Posted on 2001-08-14
3
217 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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

757 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now