converting dates in java! Please help

Discussion in 'Mac Programming' started by prostuff1, Apr 6, 2006.

  1. macrumors 65816

    prostuff1

    Joined:
    Jul 29, 2005
    Location:
    Don't step into the kawoosh...
    #1
    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.

    PHP:
    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
     
  2. Moderator emeritus

    Joined:
    Jun 25, 2002
    Location:
    Gone but not forgotten.
    #2
    I'd suggest looking at the Calendar and DateFormat classes.
     
  3. macrumors 68040

    plinden

    Joined:
    Apr 8, 2004
    #3
    I suggest looking at SimpleDateFormat as well as DateFormat

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

    Joined:
    Jun 25, 2002
    Location:
    Gone but not forgotten.
    #4
    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.
     
  5. thread starter macrumors 65816

    prostuff1

    Joined:
    Jul 29, 2005
    Location:
    Don't step into the kawoosh...
    #5
    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.
     
  6. macrumors G4

    Eraserhead

    Joined:
    Nov 3, 2005
    Location:
    UK
    #6
    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)
     
  7. macrumors 603

    gekko513

    Joined:
    Oct 16, 2003
    #7
    Eraserhead: You forgot to switch the dd and mm around.

    Solution using Scanner without error checking:
    Code:
    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);
    
     
  8. macrumors G4

    Eraserhead

    Joined:
    Nov 3, 2005
    Location:
    UK
    #8
    :p :p :p thats because I'm British and we write dates the sensible way round ;)
     
  9. macrumors 68040

    plinden

    Joined:
    Apr 8, 2004
    #9
    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.
     
  10. thread starter macrumors 65816

    prostuff1

    Joined:
    Jul 29, 2005
    Location:
    Don't step into the kawoosh...
    #10
    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:
    PHP:
    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.
     

Share This Page