• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 474
  • Last Modified:

Using Java enter string get variables day month year.

I have completed almost all of my assignment but the last part has me stuck. I need to be able to input a Date string and then convert it to a day month and year.
For example. 02121973
int day = 02
int month = 12
int year = 1973
I know how to parse the string to an integer but not break it up. I need to do it this way because in my class I need to have a private variable birthDate and two methods setBirthDate() and getBirthDate(). I also have a seperate Date class that I have to use to print the format mm/dd/yyyy from. I did not include code because I think I am close on this and if I could get the String broken down I think I will be good. Any help would be great. Thanks
0
ltzorger
Asked:
ltzorger
  • 3
  • 2
1 Solution
 
objectsCommented:
0
 
objectsCommented:
Yo can then use the Calendar class to extract the various components.

An alternative would be using the split() method of String to break it into its components.

0
 
oleberCommented:
it is this???
public class Test {
	public static void main(String args[]) {
		String str = "02121973";
		System.out.println(str.substring(0, 2));
		System.out.println(str.substring(2, 4));
		System.out.println(str.substring(4));
	}
}

Open in new window

0
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.

 
oleberCommented:
if you just need substring, the previous code shall work.
0
 
ltzorgerAuthor Commented:
Thanks for the help I acually was just getting ready to post that I had the answer. It just wouldnt work for me because I misunderstood the (int, int) part as (start, read for this many chars) and not (start, stop). works great I can even have the user place the / and then just adjust the stop and start around it. I know there is safer ways to do this because I do not control how the user inputs data, which if wrong would crash the program. But it will work for what I want to do, till I get better thanks again.
0
 
oleberCommented:
I have a similar code in another programming language: trys to gess a date for many peculiar formats.

but I have to say that some gesses are dangerous, for example:
Match's: '2121973' 2/12/1973       Is this correct?
Match's: '2121973' 21/2/1973       Or is this better?
import java.util.regex.Matcher;
import java.util.regex.Pattern;
 
public class Test {
	public static void main(String args[]) {
        String array[] = new String[]{"02121973","02/12/1973","02\\12\\1973","2-12-1973","02 - 12 /1973","2121973"};
        Pattern ptn = Pattern.compile("(\\d{1,2}?)(\\s*[-\\\\/]?\\s*)(\\d{1,2}?)(\\s*[-\\\\/]?\\s*)(\\d{4})");
        for (String str : array) {
        	Matcher matcher = ptn.matcher(str);
        	if (matcher.matches()) {
        		System.out.println("Match's: '" + str + "' " + matcher.group(1) + "/" + matcher.group(3) + "/" + matcher.group(5));
        	} else {
        		System.out.println("Doesn't match: " + str);
        	}
        }		  
    }
}

Open in new window

0

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now