Results 1 to 5 of 5
  1. #1
    stuckonjava is offline Senior Member
    Join Date
    Jan 2012
    Posts
    151
    Rep Power
    3

    Default getting todays date on java

    I need the month and day in Integer form, this is what I got: sorry for the bad variable names, this was just a test

    import java.util.Date;
    import java.util.StringTokenizer;
    import java.text.DateFormat;
    import java.text.SimpleDateFormat;


    public class GetCurrentDateTime {
    public static void main(String[] args) {

    DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
    //get current date time with Date()
    Date date = new Date();
    StringTokenizer st = new StringTokenizer(dateFormat.format(date),"/");
    while(st.hasMoreTokens()){
    String day = st.nextToken().toString();
    String month = st.nextToken().toString();

    int dayy = Integer.parseInt(day);
    int monthh = Integer.parseInt(month);

    Integer dayyy = new Integer(dayy);
    Integer monthhh = new Integer(monthh);



    when I print for example dayyy i get an error, could someone explain how I over come this

  2. #2
    gauravfzd's Avatar
    gauravfzd is offline Member
    Join Date
    Oct 2010
    Location
    India
    Posts
    72
    Rep Power
    0

    Default Re: getting todays date on java

    Quote Originally Posted by stuckonjava View Post
    I need the month and day in Integer form, this is what I got: sorry for the bad variable names, this was just a test

    import java.util.Date;
    import java.util.StringTokenizer;
    import java.text.DateFormat;
    import java.text.SimpleDateFormat;


    public class GetCurrentDateTime {
    public static void main(String[] args) {

    DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
    //get current date time with Date()
    Date date = new Date();
    StringTokenizer st = new StringTokenizer(dateFormat.format(date),"/");
    while(st.hasMoreTokens()){
    String day = st.nextToken().toString();
    String month = st.nextToken().toString();

    int dayy = Integer.parseInt(day);
    int monthh = Integer.parseInt(month);

    Integer dayyy = new Integer(dayy);
    Integer monthhh = new Integer(monthh);



    when I print for example dayyy i get an error, could someone explain how I over come this
    Try using the Calendar class (I suppose it is in java.util package well I don't remember)to extract days and month from the date object. It extracts months from 0(January) to 11(December).
    Look for the Java SE API to find the appropriate methods.....

  3. #3
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    757
    Rep Power
    8

    Default Re: getting todays date on java

    It give you an error because it failed to get the next token on the second loop. The token will only have three values, date, month and year. You take two tokens on the first loop, this left one token. On the next loop you tried to read another two token but only one is available.

    Btw, using the Calender object can make your code simpler to get the individual value of a date. One note that the Calender object month started with month = 0 for January.

    Java Code:
    Calendar calendar = Calendar.getInstance();
    int date = Calendar.get(Calendar.DATE);
    ...
    ...

  4. #4
    stuckonjava is offline Senior Member
    Join Date
    Jan 2012
    Posts
    151
    Rep Power
    3

    Default Re: getting todays date on java

    Quote Originally Posted by wsaryada View Post
    It give you an error because it failed to get the next token on the second loop. The token will only have three values, date, month and year. You take two tokens on the first loop, this left one token. On the next loop you tried to read another two token but only one is available.

    Btw, using the Calender object can make your code simpler to get the individual value of a date. One note that the Calender object month started with month = 0 for January.

    Could you explain how I use the calendar class, sorry I'm a bit confused
    Java Code:
    Calendar calendar = Calendar.getInstance();
    int date = Calendar.get(Calendar.DATE);
    ...
    ...

    Hi thanks for this, sorted all my problems out but one. I am now using the calendar object. Basically I have three combo boxes, one is integer from 1-31, the other is integer form 1970-2012 and the other is string from jan to dec. I have set the year and date to the current date however the month is stuck on"Jan" could you help me change the default month to to the current month?

    Thanks

    No worries, all Sorted, i was using setSelectedItem instead of setSelectedIndex - my bad. Thanks
    Last edited by stuckonjava; 03-25-2012 at 01:30 PM.

  5. #5
    gauravfzd's Avatar
    gauravfzd is offline Member
    Join Date
    Oct 2010
    Location
    India
    Posts
    72
    Rep Power
    0

    Default Re: getting todays date on java

    try to set the selected index of the combobox as the current month you are getting from the Calendar class.

    If you are not a die hard fan of combo box for picking dates, you can use JXDatePicker class available in SwingX library and can download from here
    SwingLabs :: Swing Component Extensions — Java.net

Similar Threads

  1. Java 7 java.util.Date vs java.sql.Date problem
    By pavel_skala in forum Advanced Java
    Replies: 0
    Last Post: 11-08-2011, 11:26 AM
  2. Replies: 4
    Last Post: 07-27-2011, 11:57 AM
  3. Todays Postings
    By camickr in forum Suggestions & Feedback
    Replies: 9
    Last Post: 05-23-2011, 05:55 AM
  4. Replies: 6
    Last Post: 04-02-2011, 01:49 PM
  5. java.util.Date vs java.sql.Date
    By Jack in forum New To Java
    Replies: 5
    Last Post: 10-28-2010, 02:59 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
  •