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
Steven Bell
660 PointsIntroduction 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
Jeremy Hill
29,567 PointsHere 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);
}
}
Jeremy Hill
29,567 PointsNo problem, I completely understand :)
Jeremy Hill
29,567 PointsI tested it before I posted it... Is it showing an error?
Steven Bell
660 PointsNo, it's just not performing the task of converting the date from stand format to European format(06.14.16).
Jeremy Hill
29,567 PointsDid you import Javax.swing.JOptionpane?
Steven Bell
660 PointsYes.
Jeremy Hill
29,567 PointsI 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?
Steven Bell
660 PointsNo, I'll try it again. Maybe I left something out.
Jeremy Hill
29,567 PointsTry 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);
}
}
Steven Bell
660 PointsLooks great, but I think it's to advanced for this class. We haven't covered IF Statements.
Thanks! Steve
Jeremy Hill
29,567 PointsOkay in a little bit I can get back on it and find a different solution for you if you would like.
Steven Bell
660 PointsYes I would!!!!
Jeremy Hill
29,567 PointsOkay 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);
}
}
Jeremy Hill
29,567 Points.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);
}
}
Steven Bell
660 PointsI 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
Jeremy Hill
29,567 PointsOh, I just realized that the numbers were switched around, Just a second.
Jeremy Hill
29,567 PointsHere, 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);
}
}
Jeremy Hill
29,567 PointsSorry about all of that; it all could have been avoided had I read the instructions better :/
Steven Bell
660 PointsHey Jeremy, that works!!! Thanks man!!!
Steve
Jeremy Hill
29,567 PointsYou'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.
Steven Bell
660 PointsSteven Bell
660 PointsThanks 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!!!!!!
Steven Bell
660 PointsSteven Bell
660 PointsHi 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.