Results 1 to 4 of 4
  1. #1
    cowboy is offline Member
    Join Date
    Oct 2009
    Posts
    48
    Rep Power
    0

    Default get date, time from jSpinner

    How i can do this ?I tryed to get this value
    Java Code:
      SpinnerDateModel model = (SpinnerDateModel)jSpinner1.getModel();
      String date_string = model.getValue().toString();
      System.out.println(date_string);
      java.util.Date date=model.getDate();
      System.out.print(date);
    But it is not exactly what i need cause i want to find only number of month .

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Don't print the Date object but rather use a SimpleDateFormat object first to format the date into a String that you want. For more on this, please have a look at the API: SimpleDateFormat API

  3. #3
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    8

    Default

    Java Code:
    import java.awt.*;
    import java.text.*;
    import java.util.*;
    import javax.swing.*;
    import javax.swing.event.*;
    
    public class Test {
        public static void main(String[] args) {
            SpinnerDateModel model = new SpinnerDateModel(new Date(), null, null,
                                                          Calendar.DAY_OF_MONTH);
            JSpinner spinner = new JSpinner(model);
            String format = "dd MMM yyyy";
            JSpinner.DateEditor editor = new JSpinner.DateEditor(spinner, format);
            spinner.setEditor(editor);
            model.addChangeListener(new ChangeListener() {
                DateFormat intFormat = new SimpleDateFormat("M");
                DateFormat strFormat = new SimpleDateFormat("MMM");
    
                public void stateChanged(ChangeEvent e) {
                    Date date = ((SpinnerDateModel)e.getSource()).getDate();
                    Calendar calendar = Calendar.getInstance();
                    calendar.setTime(date);
                    int monthIndex = calendar.get(Calendar.MONTH);
                    System.out.printf("monthIndex: %d  month: %s  %s%n",
                                       monthIndex, 
                                       intFormat.format(date),
                                       strFormat.format(date));
                }
            });
            JPanel panel = new JPanel(new GridBagLayout());
            panel.add(spinner, new GridBagConstraints());
        
            JFrame f = new JFrame();
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.add(panel);
            f.setSize(200,100);
            f.setLocation(200,200);
            f.setVisible(true);
        }
    }

  4. #4
    cowboy is offline Member
    Join Date
    Oct 2009
    Posts
    48
    Rep Power
    0

    Default

    thanks GARDWIRED. Getting instance of a CAlender - it is a best way to resolve this problem

Similar Threads

  1. [SOLVED] time and date
    By Manfizy in forum New To Java
    Replies: 9
    Last Post: 06-02-2009, 11:13 AM
  2. Time and Date
    By Manfizy in forum New To Java
    Replies: 0
    Last Post: 05-27-2009, 12:58 PM
  3. Date/Time Servlet
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-14-2008, 09:34 AM
  4. Time and Date in Java
    By java_fun2007 in forum New To Java
    Replies: 4
    Last Post: 11-06-2007, 07:25 PM
  5. how to get the current date and time
    By valery in forum New To Java
    Replies: 1
    Last Post: 08-03-2007, 06:05 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
  •