Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: how to get each field of a current date

  1. #1
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default how to get each field of a current date

    i want to extract the MONTH, YEAR and DAY of a current date

    i dont have problems with getting the current date..

    but my problem is how to extract each field of that date

    example:
    this code displays the current time
    GregorianCalendar gCal = new GregorianCalendar();
     
    System.out.println(gCal.getTime());

    what i want is to extract each day for comparing...

    i need this algorithm for my program that TESTS(compares) the current date to a GIVEN expiration Date

    pseudocode:
    if this year is less than the expiration year
    notify the user about the year
     
    if this month is 6 months less than the expiration month
    notify the user about the month
     
    if this day is 206 days less than the expiration day
    notify the user about the day

    i need some help here while im searching for a method that can do this job... tnx in advance!!!


  2. #2
    Junior Member
    Join Date
    Jan 2010
    Posts
    17
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: how to get each field of a current date

    Have a look at the link under and I'm sure you'll find what you need. 8)

    Date (Java 2 Platform SE v1.4.2)

  3. #3
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Re: how to get each field of a current date

    most of the methods of the date class is deprecated... i dont know whats the purpose of that..
    but is it still safe to use those methods?

  4. #4
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Re: how to get each field of a current date

    public class ExpirationDate {
     
        // the object for the instance of the date
        private GregorianCalendar gCal;
     
        // format for the full display of the given expiration date
        private SimpleDateFormat fullExpirationDate;
     
        // numerical format for the given expiration year
        private SimpleDateFormat yearInteger;
     
        // numerical format for the given expiration month
        private SimpleDateFormat monthInteger;
     
        // short string format of the given expiration month
        private SimpleDateFormat monthShortString;
     
        // full string format of the given expiration month
        private SimpleDateFormat monthFullString;
     
        // numerical format of the given expiration day in year
        private SimpleDateFormat dayInYearInteger;
     
        // numerical format of the given expiration day in month
        private SimpleDateFormat dayInMonthInteger;
     
        // short string format of the given expiration day in week
        private SimpleDateFormat dayInWeekShortString;
     
        // full string format of the given expiration day in week
        private SimpleDateFormat dayInWeekFullString;
     
        protected ExpirationDate() {
     
            yearInteger = new SimpleDateFormat("yy");
            monthInteger = new SimpleDateFormat("MM");
            monthShortString = new SimpleDateFormat("MMM");
            monthFullString = new SimpleDateFormat("MMMMM");
            dayInYearInteger = new SimpleDateFormat("DDD");
            dayInMonthInteger = new SimpleDateFormat("dd");
            dayInWeekShortString = new SimpleDateFormat("E");
            dayInWeekFullString = new SimpleDateFormat("EEEE");
        }
     
        /**
         * Returns a full value of the given expiration date.
         * 
         * @return a full value of this expiration date
         */
        public ExpirationDate getFullExpirationDate() {
     
            ExpirationDate eD = new ExpirationDate();
     
            eD.setExpirationDate(Integer.parseInt(this.getExpirationYearInNumber()),
                                 Integer.parseInt(this.getExpirationMonthInNumber()),
                                 Integer.parseInt(this.getExpirationDayInMonthInDigits()));
     
            return eD;
        }
     
        /**
         * @return the full string format of the given expiration date
         */
        public String toString() {
     
            return this.getExpirationMonthFullString() + ", " + this.getExpirationDayInMonthInDigits() + ", " +
                   this.getExpirationYearInNumber() + " - " + this.getExpirationDayInWeekFullString();
        }
     
        public void setExpirationDate(int year, int month, int day) {
     
     
            gCal = new GregorianCalendar(year, month - 1, day);
        }
     
        /**
         * Returns the year format of the given year, the return value of this
         * SimpleDateFormat object is only the last two digits of the given year,
         * so there is an additional first 2 digits to represent a full string
         * representation of a given year.
         * 
         * @return the given expiration year 
         */
        public String getExpirationYearInNumber() {
     
            return "20" + yearInteger.format(gCal.getTime());
        }
     
        /**
         * Returns the numerical representaion of the given expiration month.
         * 
         * @return the given expiration month in two digits
         */
        public String getExpirationMonthInNumber() {
     
            return monthInteger.format(gCal.getTime());
        }
     
        /**
         * Returns a short string representaion of the given expiration month.
         *
         * Example: October -->> oct
         * 
         * @return a short strin representaion of the given expiration month
         */
        public String getExpirationMonthShortString() {
     
            return monthShortString.format(gCal.getTime());
        }
     
        /**
         * Returns the full string representation of the given expiration month.
         * 
         * @return the full representaion of the given expiration month
         */
        public String getExpirationMonthFullString() {
     
            return monthFullString.format(gCal.getTime());
        }
     
        /**
         * Returns the expiration day in year in numerical format.
         * 
         * @return the numerical representation of the expiration day in year in
         *         3 digits
         */
        public String getExpirationDayInYearInNumber() {
     
            return dayInYearInteger.format(gCal.getTime());
        }
     
        /**
         * Returns the expiration day in month in numerical format.
         * 
         * @return the numerical representaion of the expiration day in month
         */
        public String getExpirationDayInMonthInDigits() {
     
            return dayInMonthInteger.format(gCal.getTime());
        }
     
        /**
         * Returns the expiration day in week in a short string representation
         *
         * Example: Saturday -->> sat
         * 
         * @return a short string representaion of the expiration day in week
         */
        public String getExpirationDayInWeekShortString() {
     
            return dayInWeekShortString.format(gCal.getTime());
        }
     
        /**
         * Returns the full string representation of the expiration day in week.
         * 
         * @return the full string representation of the expiration day in week
         */
        public String getExpirationDayInWeekFullString() {
     
            return dayInWeekFullString.format(gCal.getTime());
        }
     
        public static void main(String[] args) {
     
            ExpirationDate eD = new ExpirationDate();
     
            eD.setExpirationDate(10, 1, 24);  // error part
     
            System.out.println(eD.getExpirationDayInWeekShortString());
        }
    }

    output is Fri... it supposed to be sunday.. in my timezone this date is for sunday jan, 24, 2010
    fri

    10, 1, 24, should be sunday,, not friday
    please help me with this....

  5. #5
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: how to get each field of a current date

    You have to love Joda Time - Java date and time API - Home though

    // Json

  6. The Following User Says Thank You to Json For This Useful Post:

    chronoz13 (January 24th, 2010)

Similar Threads

  1. how to add a new entry in a current array
    By chronoz13 in forum Java Theory & Questions
    Replies: 1
    Last Post: December 28th, 2009, 06:39 PM
  2. How to set expiry date for cms?
    By khodam in forum JavaServer Pages: JSP & JSTL
    Replies: 3
    Last Post: December 4th, 2009, 01:15 PM
  3. Program to print current directory path to the console
    By JavaPF in forum Java Programming Tutorials
    Replies: 1
    Last Post: October 9th, 2009, 01:59 PM
  4. [SOLVED] find the position of the field separator in the String---need help ASAP
    By rajesh.mv in forum Java Theory & Questions
    Replies: 6
    Last Post: August 17th, 2009, 11:33 AM
  5. How to Get the current date and time
    By JavaPF in forum Java Programming Tutorials
    Replies: 2
    Last Post: December 2nd, 2008, 01:55 PM