Results 1 to 9 of 9
  1. #1
    kian_hong2000 is offline Member
    Join Date
    Aug 2008
    Posts
    15
    Rep Power
    0

    Default Which class and variable to choose?

    Say now i want to key in an input of 950, which class should i refer to so that i can convert it to 21:50 time?

    Should the input variable be declared as string or integer would be more suitable?

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    Since you have to do a separation, better to deal with strings according to your requirements. But in later, if you rally want to deal with the Data format, you should convert it to Date/Time format too.

  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.util.*;
    import java.text.*;
    
    public class Test {
        public static void main(String[] args) {
            DateFormat format = new SimpleDateFormat("HH:mm dd MMM yyyy");
            DateFormat timeFormat = new SimpleDateFormat("HH:mm");
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(new Date());
            Date now = calendar.getTime();
            System.out.printf("now = %s%n", format.format(now));
            // Set the time for 2150.
            int year = calendar.get(Calendar.YEAR);
            int month = calendar.get(Calendar.MONTH);
            int day = calendar.get(Calendar.DAY_OF_MONTH);
            calendar.set(year, month, day, 21, 50);
            System.out.printf("setTime = %s  time = %s%n",
                               format.format(calendar.getTime()),
                               timeFormat.format(calendar.getTime()));
        }
    }

  4. #4
    kian_hong2000 is offline Member
    Join Date
    Aug 2008
    Posts
    15
    Rep Power
    0

    Default

    1)
    How you get the SimpleDateFormat() constructor? I thought have to follow the name of the class which is DataFormat() when declaring?


    2)
    There is no Date() method in the Calendar Class. How you get it?

    My Java concept of OO is still new. Thanks!

  5. #5
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    Quote Originally Posted by kian_hong2000 View Post
    1)
    How you get the SimpleDateFormat() constructor? I thought have to follow the name of the class which is DataFormat() when declaring?
    SimpleDateFormat() constructor define from the SimpleDateFormat class. There are four overloaded constructors available. Read the doc.

    Quote Originally Posted by kian_hong2000 View Post
    2)
    There is no Date() method in the Calendar Class. How you get it?

    My Java concept of OO is still new. Thanks!
    Again, Date() is not a method, technically. That is the default constructor of the Date class.

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

    Default

    1)How you get the SimpleDateFormat() constructor? I thought have to follow the name of the class which is DataFormat() when declaring?
    Generally, yes, but DateFormat is an interface, not a class. It is implemented by the SimpleDateFormat class.

    2)There is no Date() method in the Calendar Class.
    Agreed.

    How you get it?
    The setTime method of the Calendar class takes a Date argument.
    To understand these things we look at the javadocs. Being able to find your way around the docs is a key to writing java code.
    Start with the Overview, look up the class/interface in the left frame, click on the link and the class api loads into the main frame. Now you can see what constructors, fields and methods are available to/in the class. Some may be inherited from superclasses.

  7. #7
    kian_hong2000 is offline Member
    Join Date
    Aug 2008
    Posts
    15
    Rep Power
    0

    Default

    How you know DataFormat is an interface not class?
    But from the API, i am only able to find it as class not interface.


    java.text
    Class DateFormat

    java.lang.Object
    extended by java.text.Format
    extended by java.text.DateFormat

    All Implemented Interfaces:
    Serializable, Cloneable

    Direct Known Subclasses:
    SimpleDateFormat

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

    Default

    How you know DataFormat is an interface not class?
    But from the API, i am only able to find it as class not interface.
    You are actually correct and hardwired is wrong here (almost). If you at the first line of the API, it says:
    Java Code:
    public abstract class DateFormat
    . So DateFormat is a class, but an abstract one, and as such it cannot be directly constructed itself but instead needs to be subclassed to be instantiated.

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

    Default

    How you know DataFormat is an interface not class?
    But from the API, i am only able to find it as class not interface.

    My error. Sorry.

Similar Threads

  1. Calling a method on original class from created class
    By kpedersen in forum Advanced Java
    Replies: 4
    Last Post: 08-20-2008, 12:25 AM
  2. Replies: 0
    Last Post: 05-13-2008, 06:16 AM
  3. Replies: 0
    Last Post: 04-04-2008, 12:15 AM
  4. Calling a variable from main to another class
    By itsme in forum New To Java
    Replies: 1
    Last Post: 12-18-2007, 03:35 PM
  5. Public class variable
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-03-2007, 09:58 AM

Posting Permissions

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