Results 1 to 10 of 10
  1. #1
    Join Date
    Jan 2010
    Posts
    80
    Rep Power
    0

    Default 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());
    		
    	}
    	}

  2. #2
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    Other than currentLocale being null (because you set it to null), what didn't work?

    -Gary-

  3. #3
    Join Date
    Jan 2010
    Posts
    80
    Rep Power
    0

    Default

    Quote Originally Posted by gcalvin View Post
    Other than currentLocale being null (because you set it to null), what didn't work?

    -Gary-
    I finally figured it out

    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 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());
    		
    	}
    	}
    I need this code to use in an applet

    when I use it, it didn't work

    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);
    	}
    }
    I want to display the current date in a label.

  4. #4
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    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-

  5. #5
    Join Date
    Jan 2010
    Posts
    80
    Rep Power
    0

    Default

    Quote Originally Posted by gcalvin View Post
    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-
    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);
    		
    	}
    }

  6. #6
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    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-

  7. #7
    Join Date
    Jan 2010
    Posts
    80
    Rep Power
    0

    Default

    Quote Originally Posted by gcalvin View Post
    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-
    I finally discovered it

    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(){
    		
    		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);
    	}
    	}
    }
    when I wrote this code, it didn't give any mistakes

    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

  8. #8
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    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-

  9. #9
    Join Date
    Jan 2010
    Posts
    80
    Rep Power
    0

    Default

    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

  10. #10
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,527
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by ŖΫ ỏ Ңόρę View Post
    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
    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

  1. Compare date input to database with current date
    By hungleon88 in forum Advanced Java
    Replies: 2
    Last Post: 11-25-2008, 08:10 AM
  2. How to print the creation date of a jar file
    By lunarbof in forum Advanced Java
    Replies: 9
    Last Post: 08-22-2008, 06:12 PM
  3. Print the text file and print preview them
    By Java Tip in forum java.awt
    Replies: 0
    Last Post: 06-22-2008, 11:04 PM
  4. Creating a Gregorian Calendar using a Date object gives date - 1
    By prachi_goliwadekar in forum New To Java
    Replies: 1
    Last Post: 05-08-2008, 08:32 PM
  5. Difference between current date and anothe date
    By vijay balusamy in forum New To Java
    Replies: 1
    Last Post: 04-16-2008, 04:15 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •