Results 1 to 10 of 10
Thread: Print out the date
- 04-03-2010, 07:33 AM #1
Member
- Join Date
- Jan 2010
- Posts
- 80
- Rep Power
- 0
Print out the date
Hello
I wrote the following code to print out the current date.
After I run it, it didn't work !!
Java Code:import java.util.Date; import java.util.Locale; import java.util.Random; import java.text.*; public class Try1 { public static void main(String[] args){ Locale currentLocale = null; Date today; String dateOut; DateFormat dateFormatter; dateFormatter = DateFormat.getDateInstance(DateFormat.DEFAULT,currentLocale); today = new Date(); dateOut = dateFormatter.format(today); System.out.println(dateOut + " " + currentLocale.toString()); } }
- 04-03-2010, 08:19 AM #2
Senior Member
- Join Date
- Mar 2010
- Posts
- 953
- Rep Power
- 4
Other than currentLocale being null (because you set it to null), what didn't work?
-Gary-
- 04-03-2010, 08:35 AM #3
Member
- Join Date
- Jan 2010
- Posts
- 80
- Rep Power
- 0
I finally figured it out
I need this code to use in an appletJava Code:import java.util.Date; import java.util.Locale; import java.util.Random; import java.text.*; public class Try1 { public static void main(String[] args){ Locale alocal = new Locale("CANADA"); Date today; String dateOut; DateFormat dateFormatter; dateFormatter = DateFormat.getDateInstance(DateFormat.DEFAULT, alocal); today = new Date(); dateOut = dateFormatter.format(today); System.out.println(dateOut + " " + alocal.toString()); } }
when I use it, it didn't work
I want to display the current date in a label.Java Code:import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.text.DateFormat; import java.util.Date; import java.util.Locale; public class Management extends JApplet implements ActionListener{ private JLabel label; private JLabel labelDate; private JButton button; private static Locale alocal = new Locale("CANADA"); private static Date today; private static String dateOut; private static DateFormat dateFormatter; public void init(){ label = new JLabel("Welcome in Management Programe"); labelDate = new JLabel(dateOut); button = new JButton("Entrance Panel"); button.addActionListener(this); Container contentPane = getContentPane(); contentPane.setLayout(new FlowLayout()); contentPane.add(label); contentPane.add(labelDate); contentPane.add(button); } public void actionPerformed(ActionEvent eve){ } public static void main(String[] args){ dateFormatter = DateFormat.getDateInstance(DateFormat.DEFAULT, alocal); today = new Date(); dateOut = dateFormatter.format(today); } }
- 04-03-2010, 08:50 AM #4
Senior Member
- Join Date
- Mar 2010
- Posts
- 953
- Rep Power
- 4
Well, "CANADA" is not a valid locale string, as far as I know, but that doesn't seem to matter much here. The bigger problem is that you initialize labelDate with the empty String dateOut, then when you point the ivar dateOut to a different String in main() you expect labelDate to get its new value. It doesn't work that way. You need to call labelDate's setText() method.
-Gary-
- 04-03-2010, 09:18 AM #5
Member
- Join Date
- Jan 2010
- Posts
- 80
- Rep Power
- 0
Hi Gary
It works properly before I use it in the applet.
I did what you said but when I run it, it gives me null instead of the date
Java Code:import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.text.DateFormat; import java.util.Date; import java.util.Locale; public class Management extends JApplet implements ActionListener{ private JLabel label; private JLabel labelDate; private JButton button; private static Locale alocal; private static Date today; private static String dateOut; private static DateFormat dateFormatter; public void init(){ label = new JLabel("Welcome in Management Programe"); labelDate = new JLabel(""); button = new JButton("Entrance Panel"); button.addActionListener(this); Container contentPane = getContentPane(); contentPane.setLayout(new FlowLayout()); contentPane.add(label); contentPane.add(labelDate); contentPane.add(button); } public void actionPerformed(ActionEvent eve){ labelDate.setText(dateOut); } public static void main(String[] args){ alocal = new Locale("CANADA"); dateFormatter = DateFormat.getDateInstance(DateFormat.DEFAULT, alocal); today = new Date(); dateOut = dateFormatter.format(today); } }
- 04-03-2010, 10:17 AM #6
Senior Member
- Join Date
- Mar 2010
- Posts
- 953
- Rep Power
- 4
The main() method doesn't get run in a JApplet (unless you explicitly run it, which you typically wouldn't). Move your main() code into init() (and study up on Locale a little more).
-Gary-
- 04-04-2010, 06:34 AM #7
Member
- Join Date
- Jan 2010
- Posts
- 80
- Rep Power
- 0
I finally discovered it
when I wrote this code, it didn't give any mistakesJava Code:import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.text.DateFormat; import java.util.Date; import java.util.Locale; public class Management extends JApplet implements ActionListener{ private JLabel label; private JLabel labelDate; private JButton button; private static Locale alocal; private static Date today; private static String dateOut; private static DateFormat dateFormatter; public void init(){ alocal = new Locale("CANADA"); dateFormatter = DateFormat.getDateInstance(DateFormat.DEFAULT, alocal); today = new Date(); dateOut = dateFormatter.format(today); label = new JLabel("Welcome in Management Programe"); button = new JButton("Entrance Panel"); labelDate = new JLabel(""); labelDate.setText(dateOut); button.addActionListener(this); Container contentPane = getContentPane(); contentPane.setLayout(new FlowLayout()); contentPane.add(label); contentPane.add(labelDate); contentPane.add(button); } public void actionPerformed(ActionEvent eve){ String str = JOptionPane.showInputDialog(getContentPane(), "Enter the user name"); if(str == "NsNs"){ JOptionPane.showMessageDialog(getContentPane(), "Welcome Mr.NsNs"); }else{ JOptionPane.showMessageDialog(getContentPane(), "Invalid Name !!","Error" ,JOptionPane.ERROR_MESSAGE); } } }
but the problem when I enter NsNs in the inputDialog it gives me "Invalid Name".
I hope you can help me with this problem
thanks
- 04-04-2010, 06:56 AM #8
Senior Member
- Join Date
- Mar 2010
- Posts
- 953
- Rep Power
- 4
That is because the String that you enter is not == to "NsNs". I will anticipate your insistence that "Yes it is!!" and respond "No, it isn't" and point you to the Java documentation.
-Gary-
- 04-04-2010, 07:03 PM #9
Member
- Join Date
- Jan 2010
- Posts
- 80
- Rep Power
- 0
Gary, you really confused me !
I enter the same string that is in the if statement, so it should be right and it should give me Welcome ..!!
could you please explain more ?
appreciate that brother
thanks
- 04-04-2010, 08:37 PM #10
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,378
- Blog Entries
- 7
- Rep Power
- 17
Did you ever read a Java tutorial/book? If you did you'd known that you can't compare objects for equal content using the == operator; sometimes it works for reasons I won't explain here but in general you can't use the == operator for those purposes as Gary already said. No need to protest, you can't use it, no matter if you are definitely, positively sure that those Strings are equal. Read a tutorial and don't guess and don't assume that Java (or any other programming language) works the way you want it to work.
kind regards,
Jos
Similar Threads
-
Compare date input to database with current date
By hungleon88 in forum Advanced JavaReplies: 2Last Post: 11-25-2008, 08:10 AM -
How to print the creation date of a jar file
By lunarbof in forum Advanced JavaReplies: 9Last Post: 08-22-2008, 06:12 PM -
Print the text file and print preview them
By Java Tip in forum java.awtReplies: 0Last Post: 06-22-2008, 11:04 PM -
Creating a Gregorian Calendar using a Date object gives date - 1
By prachi_goliwadekar in forum New To JavaReplies: 1Last Post: 05-08-2008, 08:32 PM -
Difference between current date and anothe date
By vijay balusamy in forum New To JavaReplies: 1Last Post: 04-16-2008, 04:15 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks