Results 1 to 7 of 7
  1. #1
    eLancaster is offline Member
    Join Date
    Jan 2011
    Posts
    93
    Rep Power
    0

    Default Is there a Java function which retrieves todays name (e.g. Friday) from the OS?

    I'm writing a GUI program which draws tick for how many days you've run the program consecutively.

    So if i first run it on sunday, it will show one tick. If i run it again on monday, two ticks. If i skip a day and run it on wednesday, it will start over and show one tick, because monday and wednesday aren't consecutive.

    Yesterday I wrote some 150 lines of code to make three combo boxes: day, month and year so that the user can enter a date and the program calculates what day is today and writes it to a file.

    I wanted to do that for the sake of practice. But now i want to know, for the sake of knowledge, could I have used some java function which would automatically retrieve todays day from the Operating System?

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,371
    Blog Entries
    7
    Rep Power
    20

    Default

    Have a look at the DateFormat class or the SimpleDateFormat class.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    UJJAL DHAR is offline Senior Member
    Join Date
    Apr 2010
    Location
    Dhaka,Bangladesh
    Posts
    180
    Rep Power
    0

    Default

    you can use this
    Java Code:
    GregorianCalendar cal = new GregorianCalendar(); //Create calendar
    		int s=cal.get(GregorianCalendar.DAY_OF_WEEK);
    S=7 in Saturday. that means week starts with Sunday

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,371
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by UJJAL DHAR View Post
    you can use this
    Java Code:
    GregorianCalendar cal = new GregorianCalendar(); //Create calendar
    		int s=cal.get(GregorianCalendar.DAY_OF_WEEK);
    S=7 in Saturday. that means week starts with Sunday
    Great, and how is the OP supposed to get the name of the week day out of that in a decent way? Better read my first reply.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    eLancaster is offline Member
    Join Date
    Jan 2011
    Posts
    93
    Rep Power
    0

    Default

    Thanks Josah, i'll have a look at that.
    Last edited by eLancaster; 04-02-2011 at 01:14 PM.

  6. #6
    UJJAL DHAR is offline Senior Member
    Join Date
    Apr 2010
    Location
    Dhaka,Bangladesh
    Posts
    180
    Rep Power
    0

    Default

    Quote Originally Posted by JosAH View Post
    Great, and how is the OP supposed to get the name of the week day out of that in a decent way? Better read my first reply.

    kind regards,

    Jos
    Yes,your reply is more eficient than that of mine.But mine one will also work if we want.

    Something Like this:

    switch(s){
    Case 1: Sunday;break;
    Case 2: Monday;break;
    .
    .
    .
    .
    Case 7: Saturday;break;
    }


    Regards
    Ujjal
    Don't Forget to try yourself before asking others help.....
    Press REP, if you find their advices/solutions effective.

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

    Default

    Quote Originally Posted by UJJAL DHAR View Post
    Yes,your reply is more eficient than that of mine.But mine one will also work if we want.

    Something Like this:

    switch(s){
    Case 1: Sunday;break;
    Case 2: Monday;break;
    .
    .
    .
    .
    Case 7: Saturday;break;
    }


    Regards
    Ujjal
    That is a very bad and hackish 'solution' (mind the quotes); for one thing you lose all localization and for another: you don't know whether or not those numbers lie in the range [1 ... 7]. The Calendar class doesn't have symbolic constants for no reason. Don't do what you just did.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. php function to java
    By thorbcn in forum New To Java
    Replies: 2
    Last Post: 07-04-2011, 12:40 AM
  2. Calling function in Javascript- from other function
    By jdigger in forum New To Java
    Replies: 1
    Last Post: 02-27-2011, 09:00 PM
  3. Friday night java puzzle
    By niu_niu in forum New To Java
    Replies: 12
    Last Post: 06-19-2010, 10:54 PM
  4. Replies: 3
    Last Post: 12-04-2009, 03:08 PM
  5. help with function in java!!!!
    By ricky in forum New To Java
    Replies: 6
    Last Post: 10-12-2009, 01:24 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
  •