The Artima Developer Community
Sponsored Link

Java Answers Forum
Date

3 replies on 1 page. Most recent reply: Feb 4, 2003 7:54 AM by Mike Penner

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 3 replies on 1 page
A E

Posts: 2
Nickname: ewhop
Registered: Dec, 2002

Date Posted: Feb 1, 2003 1:14 PM
Reply to this message Reply
Advertisement
I read in from the keyboard a value as a string which is suposed to be a date. I want to validate if this string is a date and i want to take out the year of this date. Anybody can help?


Mike Penner

Posts: 16
Nickname: mikepenner
Registered: Jan, 2003

Re: Date Posted: Feb 3, 2003 12:14 PM
Reply to this message Reply
Have a look at java.text.DateFormat's parse method. It returns a java.util.Date object, which has a getYear method. However, Date.getYear() is deprecated and probably doesn't do exactly what you want (YY v. YYYY). java.util.Calendar has a method that does what you want to do: Calendar.get(Calendar.YEAR). Note that you'll have to use Calendar's concrete subclass, java.util.GregorianCalendar to create the Calendar object. Note GregorianCalendar's setTime method.

So what you need to do is something like:

1. Use DateFormat to get a Date object from a String
2. Create a new GregorianCalendar object and initialize it to the Date
3. Call get(Calendar.YEAR) on your GregorianCalendar to get the year.

Charles Bell

Posts: 519
Nickname: charles
Registered: Feb, 2002

Re: Date Posted: Feb 3, 2003 3:42 PM
Reply to this message Reply
The string a user can enter from the keyboard can have many different formats.

You may want to prompt the user, with an example of the format you have programmed your application for.

import java.io.*;
 
public class Test{
    
    public Test(){
        
    }
    
    public static void main (String[] args){
        Test test = new Test();
        String dateString = test.getDateString();
        if (test.validateDateString(dateString)){
            System.out.println(dateString);
        }else{
            System.out.println("Invalidate date entered.");            
        }
    }
    
 
    
    public String readConsoleInput(){
        String lineRead = "";
        try{
            InputStreamReader isr = new InputStreamReader(System.in); 
            BufferedReader br = new BufferedReader(isr);
            lineRead = br.readLine();
        }catch(IOException ioe){
            System.err.println("IOException: " + ioe.getMessage());
        }
        return lineRead;        
    }
    
    public String getDateString(){
        String lineRead = "";
        try{
            System.out.println("Enter Date: MM-DD-YYYY");
            InputStreamReader isr = new InputStreamReader(System.in); 
            BufferedReader br = new BufferedReader(isr);
            lineRead = br.readLine();
        }catch(IOException ioe){
            System.err.println("IOException: " + ioe.getMessage());
        }
        return lineRead;        
    }
    
    public boolean validateDateString(String dateString){
        int month = toInt(getMonth(dateString));
        int day = toInt(getDay(dateString));
        int year = toInt(getYear(dateString));
        System.out.println("Month: " + month);
        System.out.println("Day: " + day);
        System.out.println("Year: " + year);
        return ((month > 0) && (month <= 12)
                && (day > 0) && (day <= 31)
                && (year > 0) && (getYear(dateString).length() == 4));
    }
    
    private String getMonth(String dateString){
        String month = "";
        if (dateString.indexOf("-") > 0) month = dateString.substring(0,dateString.indexOf("-"));
        return month;        
    }
 
    private String getDay(String dateString){
        String day = "";
        if (dateString.indexOf("-") > 0) day = dateString.substring(dateString.indexOf("-") + 1,dateString.lastIndexOf("-"));
        return day;        
    }
 
    private String getYear(String dateString){
        String year = "";
        if (dateString.indexOf("-") > 0) year = dateString.substring(dateString.lastIndexOf("-")+1);
        return year;        
    }
    
    
    private int toInt(String s){
        int n = -1;
        try{
            n = Integer.parseInt(s);
        }catch(NumberFormatException nfe){
            System.err.println(s + " is not a valid integer.");
        }
        return n;
    }
}


This has an output which looks like:
Enter Date: MM-DD-YYYY
12-22-2003
Month: 12
Day: 22
Year: 2003
12-22-2003

Mike Penner

Posts: 16
Nickname: mikepenner
Registered: Jan, 2003

Re: Date Posted: Feb 4, 2003 7:54 AM
Reply to this message Reply
If you know the format of the input date, SimpleDateFormat will do the parsing for you.

String format = "yyyy-MM-dd";
String inputDate = "2003-12-25";
SimpleDateFormat sdf = new SimpleDateFormat(format);
GregorianCalendar gc = new GregorianCalendar();
gc.setTime(sdf.parse(inputDate));

Flat View: This topic has 3 replies on 1 page
Topic: How can I knoy the machine name where the program is running? Previous Topic   Next Topic Topic: GetDate function

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use