Solved

How do I cast a string to a date object

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
micro services vs rest web services 16 107
HTTPSessionBindingListernter not configured in deployment descriptor 2 53
varialbe initialization 11 33
Chrome and Firefox Java 5 34
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 …
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
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.

803 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