PDA

View Full Version : converting dates in java! Please help




prostuff1
Apr 7, 2006, 01:22 AM
I have just started my java programming classes and i am trying to finish my first project.

I need some help figuring this out.

What i need to do is input the date in the format mm/dd/yyyy and then it will output it as dd-mm-yyyy.

This is probably a really simple problem that i am overlooking(or just to stupid to figure out...this is only my second java programming lab). Anyway is someone could help me out and possibly explain why i have to do those steps to get it to work that would be great. I did a search on google but that really did not help all that much.

I am going to show you the whole code for the project right now but really all you need is the stuff at the end.

import java.util.Scanner;
import java.util.*;
import java.text.*;
public class Lab2
{
public static void main(String[] args)
{
Scanner keyboard = new Scanner(System.in);

System.out.println("***************************************");
System.out.println("* Welcome to your first Java program! *");
System.out.println("***************************************");
System.out.println(" ");
System.out.println("*** Test integer arithmetic ***");
System.out.println(" ");
System.out.println("Enter first integer number: ");
int value1 = keyboard.nextInt();
System.out.println("Enter second integer number: ");
int value2 = keyboard.nextInt();
int addvalue = value1 + value2;
System.out.println(value1 + " + " + value2 + " = " + addvalue);
int minusvalue = value1 - value2;
System.out.println(value1 + " - " + value2 + " = " + minusvalue);
int multiplyvalue = value1 * value2;
System.out.println(value1 + " * " + value2 + " = " + multiplyvalue);
int dividevalue = value1 / value2;
System.out.println(value1 + " / " + value2 + " = " + dividevalue);
int modulusvalue = value1 % value2;
System.out.println(value1 + " % " + value2 + " = " + modulusvalue);
System.out.println(" ");
System.out.println("*** Test real arithmetic ***");
System.out.println(" ");
System.out.println("Enter first real number: ");
double real1 = keyboard.nextDouble();
System.out.println("Enter second real number: ");
double real2 = keyboard.nextDouble();
double addreal = real1 + real2;
System.out.println(real1 + " + " + real2 + " = " + addreal);
double minusreal = real1 - real2;
System.out.println(real1 + " - " + real2 + " = " + minusreal);
double multiplyreal = real1 * real2;
System.out.println(real1 + " * " + real2 + " = " + multiplyreal);
double dividereal = real1 / real2;
System.out.println(real1 + " / " + real2 + " = " + dividereal);
System.out.println(" ");
System.out.println("*** Test String operations ***");
System.out.println(" ");
keyboard.nextLine();
System.out.println("Enter a string of characters: ");
String typedwords = keyboard.nextLine();
System.out.println("The length of string"+ " \"" + typedwords + "\"" +
" is " + typedwords.length());
System.out.println("Enter an integer between 0 and 20: ");
int stringint = keyboard.nextInt();
System.out.println("The character at index " + stringint +
" of string " + "\"" + typedwords + "\"" + " is " +
"\'" + typedwords.charAt(stringint) + "\'");
keyboard.nextLine();
System.out.println("Enter another string of characters: ");
String typedword = keyboard.nextLine();
System.out.println("The first occurence of string " + "\"" +
typedword + "\"" + " in string " + "\"" + typedwords +
"\"" + " is at position " + typedwords.indexOf(typedword));
System.out.println(" ");
System.out.println("*** One last test ***");
System.out.println(" ");
System.out.println("Enter your birthday (mm/dd/yyyy): ");




}
}


Please help me out!!

Thanks



bousozoku
Apr 7, 2006, 01:30 AM
I'd suggest looking at the Calendar and DateFormat classes.

plinden
Apr 7, 2006, 01:34 AM
I suggest looking at SimpleDateFormat (http://java.sun.com/j2se/1.4.2/docs/api/java/text/SimpleDateFormat.html) as well as DateFormat (http://java.sun.com/j2se/1.4.2/docs/api/java/text/DateFormat.html)

Hint, use the parse method to get the Date object, then the format method to format the output.

bousozoku
Apr 7, 2006, 02:10 AM
I suggest looking at SimpleDateFormat (http://java.sun.com/j2se/1.4.2/docs/api/java/text/SimpleDateFormat.html) as well as DateFormat (http://java.sun.com/j2se/1.4.2/docs/api/java/text/DateFormat.html)

Hint, use the parse method to get the Date object, then the format method to format the output.

Very good ideas and even better, the fact that you didn't do the research for him. It's far too easy to request the answer and have someone give it to you, but Java is all about research and you don't find anything until you've dug through the documents.

By the way, another suggestion: O'Reilly's Java in a Nutshell. It's the book that is both a reference and a tutorial of sorts, full of examples.

prostuff1
Apr 7, 2006, 02:13 AM
Very good ideas and even better, the fact that you didn't do the research for him. It's far too easy to request the answer and have someone give it to you, but Java is all about research and you don't find anything until you've dug through the documents.

By the way, another suggestion: O'Reilly's Java in a Nutshell. It's the book that is both a reference and a tutorial of sorts, full of examples.

Thanks for pointing out the book. I really do learn better from examples then just stuff being shoved at me and saying figure it out. While it is satisfying when you do figure it out i learn better from examples.

Thanks for a point in the right direction guys.

Eraserhead
Apr 7, 2006, 04:26 AM
If you convert it to a string, its really easy to switch the formats, using the 'replace' method in the String API.

Search google for 'String +java' for more info.

import java.util.Scanner;

public class test {
public static void main (String args[]) {
//insert your other code here.

// reading in the data
Scanner myScanner=new Scanner(System.in);
System.out.println("Please enter your birthday (dd/mm/yyyy)");
String theDate=myScanner.next();
theDate=theDate.replace("/","-");
System.out.println(theDate);}}
If uses a different method of reading in data, and doesn't check for errors in the date (eg if you entered Frog it'd return Frog)

gekko513
Apr 7, 2006, 08:14 AM
Eraserhead: You forgot to switch the dd and mm around.

Solution using Scanner without error checking:

String mm = keyboard.next("\d*");
keyboard.skip("/");
String dd = keyboard.next("\d*");
keyboard.skip("/");
String yyyy = keyboard.next("\d*");
System.out.println(dd+"-"+mm+"-"+yyyy);

Eraserhead
Apr 7, 2006, 08:59 AM
Eraserhead: You forgot to switch the dd and mm around.

:p :p :p thats because I'm British and we write dates the sensible way round ;)

plinden
Apr 7, 2006, 10:31 AM
If you convert it to a string, its really easy to switch the formats, using the 'replace' method in the String API.

Search google for 'String +java' for more info.

import java.util.Scanner;

public class test {
public static void main (String args[]) {
//insert your other code here.

// reading in the data
Scanner myScanner=new Scanner(System.in);
System.out.println("Please enter your birthday (dd/mm/yyyy)");
String theDate=myScanner.next();
theDate=theDate.replace("/","-");
System.out.println(theDate);}}
If uses a different method of reading in data, and doesn't check for errors in the date (eg if you entered Frog it'd return Frog)

Sorry, but that's a kludge and not very configurable. Example - write a program to read a properties file to get the output date format, or take it from the command line. Character replacement doesn't handle that very well.

prostuff1
Apr 7, 2006, 10:58 AM
I looked through all the info you gave me and while i was doing that i was thinking to myself that this was stuff we had not learned yet. It would get the job done and probably better then the way i came up with but my way does work.

Here is what i ended up doing:
System.out.println("Enter your birthday (mm/dd/yyyy): ");
String birthday = keyboard.nextLine();
System.out.println("You were born on " + birthday.substring(3,5) +
"-" + birthday.substring(0,2) + "-" + birthday.substring(6,10));

We have not learned the date stuff yet so i figured i should stay with stuff we had.

Thanks for the help guys. When it comes to dates i should be ahead of the game.