Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java

Introduction to Java Exercise

Write a program that reads a string for a date in the format month / day / year and displays it in the format day . month . year, which is a typical format used in Europe. For example, if the input is 06/17/08, the output should be 17.06.08. Your program should use JOptionPane for input and output.

13 Answers

Here ya go:

import javax.swing.JOptionPane;

public class Example{

    public static void main(String[] args){

       String newDate = "";
       String date = JOptionPane.showInputDialog(null, "Enter the date: ","Date Converter", JOptionPane.PLAIN_MESSAGE); 
       newDate = date.replace('/', '.');
       JOptionPane.showMessageDialog(null, "Here is the new formatted date: "+newDate);

    }
}

Thanks very much Jeremy! I'm trying to get the hang of writing Java programs but my instructor keeps throwing me off! Have patience with me!!!!!!

Hi Jeremy,

The program doesn't seem to convert the date from mm/dd/yy to dd.mm.yy. I tested it but couldn't get it to work.

No problem, I completely understand :)

I tested it before I posted it... Is it showing an error?

No, it's just not performing the task of converting the date from stand format to European format(06.14.16).

Did you import Javax.swing.JOptionpane?

Yes.

I just ran the code again and it worked: it doesn't have the zero in front of the month; is that what you are talking about?

No, I'll try it again. Maybe I left something out.

Try this. I just tweaked the code, again it worked on my computer, but let me know if it works:

import javax.swing.JOptionPane;

public class Example{

    public static void main(String[] args){

       String newDate = "";
       String date = JOptionPane.showInputDialog(null, "Enter the date: ","Date Converter", JOptionPane.PLAIN_MESSAGE);
       String[] temp = date.split("/");
       for(int i = 0; i < temp.length; i++){

           if(temp[i].length() == 1){
               temp[i] = "0"+temp[i];
           }
           if(i != temp.length-1)
             newDate += temp[i]+".";
           else
               newDate += temp[i];
       }

       //newDate = date.replace('/', '.');
       JOptionPane.showMessageDialog(null, "Here is the new formatted date: "+newDate);

    }
}

Looks great, but I think it's to advanced for this class. We haven't covered IF Statements.

Thanks! Steve

Okay in a little bit I can get back on it and find a different solution for you if you would like.

Yes I would!!!!

Okay here is the code again without the if statements; it's similar to the first code that I posted, but give it a shot and let me know.

import javax.swing.JOptionPane;

public class Example{

    public static void main(String[] args){

       String newDate = "";
       String date = JOptionPane.showInputDialog(null, "Enter the date: ","Date Converter", JOptionPane.PLAIN_MESSAGE);
       //String[] temp = date.split("/");
       newDate = date.replaceAll("/", ".");

       //newDate = date.replace('/', '.');
       JOptionPane.showMessageDialog(null, "Here is the new formatted date: "+newDate);

    }
}

.and if that still doesn't work you can use this:

import javax.swing.JOptionPane;

public class Example{

    public static void main(String[] args){

       String newDate = "";
       String date = JOptionPane.showInputDialog(null, "Enter the date: ","Date Converter", JOptionPane.PLAIN_MESSAGE);
       String[] temp = date.split("/");
       for(String string : temp){
           newDate += string + '.';
       }
       JOptionPane.showMessageDialog(null, "Here is the new formatted date:  "+newDate);

    }
}

I appreciate your efforts Jeremy, but it's still not working. It's replacing the / with . but not changing 06/14/16 into 14.6.16.

Steve

Oh, I just realized that the numbers were switched around, Just a second.

Here, this should fix it.

import javax.swing.JOptionPane;

public class Example{

    public static void main(String[] args){

        // from  6/14/16  to  14.6.16
       String newDate = "";
       String date = JOptionPane.showInputDialog(null, "Enter the date: ","Date Converter", JOptionPane.PLAIN_MESSAGE);
       String[] temp = date.split("/");
       newDate = temp[1]+".";
       newDate += temp[0]+".";
       newDate += temp[2];
       JOptionPane.showMessageDialog(null, "Here is the new formatted date:  "+newDate);

    }
}

Sorry about all of that; it all could have been avoided had I read the instructions better :/

Hey Jeremy, that works!!! Thanks man!!!

Steve

You're welcome, the whole time I thought it was just the decimals that you were needing in there, it wasn't until your last reply that I realized that- my bad.