Results 1 to 2 of 2
  1. #1
    valoyivd is offline Member
    Join Date
    Mar 2008
    Posts
    8
    Rep Power
    0

    Default java Date and Calendar

    I am new to Java , I have written this method in order to achieve the following!!!

    I want to Calculate current date + 30 days. If calculated date = 1st of a month then use this date. If it is not the 1st of a month, then use the 1st of the following month.
    Example…if today is 15 April 2007, then expiry date will be 1 June 2007.
    If today is 2 May 2007, then expiry date will be 1 June 2007.

    I want to simplify this code so that It looks pretty...
    please help me to do that!!!!



    Java Code:
    import java.util.Calendar;
     
    /**
     * @author valoyi
     *
     */
    public class CalcUti {
    	@SuppressWarnings("unused")
    	private static String getExpiaryDate(){
    		Calendar calendar = Calendar.getInstance();
    		calendar.add(Calendar.DAY_OF_MONTH, 30);
    		 int day = calendar.get(Calendar.DAY_OF_MONTH);
    		 if(day == 1){
    			 return ""+ calendar;
    		 }
    		 else{
    			 calendar.add(Calendar.MONTH,2);
    			 calendar.set(Calendar.DAY_OF_MONTH,1);
    			 return " " + calendar;
    		 }
    		
    	}
     
    }

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

    Default

    Java Code:
    import java.text.*;
    import java.util.*;
    
    public class Test {
        static DateFormat df = new SimpleDateFormat("dd MMM yyyy");
        static int[][] fields = {
            { 2007, 3, 15 }, { 2007, 4, 2 }, { 2007, 11, 12 }
        };
    
        public static void main(String[] args) {
            for(int i = 0; i < fields.length; i++) {
                Calendar calendar = Calendar.getInstance();
                int[] n = fields[i];
                calendar.set(n[0], n[1], n[2]);
                System.out.printf("%s = %s%n",
                                   Arrays.toString(fields[i]),
                                   df.format(calendar.getTime()));
                System.out.printf("expiryDate = %s%n",
                                   getExpiaryDate(calendar.getTime()));
            }
        }
     
        private static String getExpiaryDate(Date date) {
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            calendar.add(Calendar.DAY_OF_MONTH, 30);
            int day = calendar.get(Calendar.DAY_OF_MONTH);
            if(day != 1) {
                calendar.add(Calendar.MONTH, 1);
                calendar.set(Calendar.DAY_OF_MONTH, 1);
            }
            return df.format(calendar.getTime());
                   //calendar.toString();
        }
    }

Similar Threads

  1. java.util.Date vs java.sql.Date
    By Jack in forum New To Java
    Replies: 5
    Last Post: 10-28-2010, 03:59 PM
  2. Calendar.DATE
    By mew in forum New To Java
    Replies: 1
    Last Post: 01-04-2008, 08:51 PM
  3. Java Calendar Class
    By vasu18 in forum New To Java
    Replies: 1
    Last Post: 12-22-2007, 03:51 AM
  4. Java Calendar Class
    By vasu18 in forum New To Java
    Replies: 0
    Last Post: 12-06-2007, 11:45 AM
  5. Making a Java web calendar
    By toby in forum Enterprise JavaBeans (EJB)
    Replies: 1
    Last Post: 08-07-2007, 07:05 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
  •