Monday, November 3, 2014

Java current date pro



Add or subtract days to current date

import java.util.Calendar;
public class AddDaysToCurrentDate
          {
public static void main(String[] args)
{
Calendar now = Calendar.getInstance();
System.out.println("Current date : " + (now.get(Calendar.Month) + 1)
+ "-"
+ now.get(Calendar.Date)
+ "-"
+ now.get(Calendar.Year));
now.add(Calendar.Date,1);

System.out.println("date after one day : " + (now.get(Calendar.Month) + 1)
+ "-"
+ now.get(Calendar.Date)
+ "-"
+ now.get(Calendar.Year));
now = Calendar.getInstance();
now.add(Calendar.Date, -10);
System.out.println("date before 10 days:"+(now.get(Calendar.Month) + 1)
+ "-"
+ now.get(Calendar.Date)
+ "-"
+ now.get(Calendar.Year));
  }
}

Add or subtract hours to current time

import java.util.Calendar;
public class AddHoursToCurrentDate
{
public static void main(String[] args)
{
Calendar now = Calendar.getInstance();
System.out.println("Current Date : " + (now.get(Calendar.Month) + 1)
+ "-"
+ now.get(Calendar.Date)
+ "-"
+ now.get(Calendar.Year));
System.out.println("Current time : " + now.get(Calendar.Hour_Of_Day)
+ ":"
+ now.get(Calendar.Minute)
+ ":"
+ now.get(Calendar.Second));
now.add(Calendar.Hour,10);
System.out.println("New time after adding 10 hours : "
+ now.get(Calendar.Hour_Of_Day)
+ ":"
+ now.get(Calendar.Minute)
+ ":"
+ now.get(Calendar.Second));
System.out.println("New date after adding 10 hours : "
+ (now.get(Calendar.Month) + 1)
+ "-"
+ now.get(Calendar.Date)
+ "-"
+ now.get(Calendar.Year));
now = Calendar.getInstance();
now.add(Calendar.Hour, -3);
System.out.println("Time before 3 hours : " + now.get(Calendar.Hour_Of_Day)
+ ":"
+ now.get(Calendar.Minute)
+ ":"
+ now.get(Calendar.Second));
  }
}

Add or subtract minutes to current time

import java.util.Calendar;
public class AddMinutesToCurrentDate
{
public static void main(String[] args)
{
Calendar now = Calendar.getInstance();
System.out.println("Current time : " + now.get(Calendar.Hour_Of_Day)
+ ":"
+ now.get(Calendar.Minute)
+ ":"
+ now.get(Calendar.Second));
now.add(Calendar.Minute,20);
System.out.println("New time after adding 20 minutes : "
+ now.get(Calendar.Hour_Of_Day)
+ ":"
+ now.get(Calendar.Minute)
+ ":"
+ now.get(Calendar.Second));
now = Calendar.getInstance();
now.add(Calendar.Minute, -50);
System.out.println("Time before 50 minutes : " + now.get(Calendar.Hour_Of_Day)
+ ":"
+ now.get(Calendar.Minute)
+ ":"
+ now.get(Calendar.Second));
    }
}

Add or subtract months to current date

import java.util.Calendar;
public class AddMonthsToCurrentDate
{
public static void main(String[] args)
{
Calendar now = Calendar.getInstance();
System.out.println("Current date : " + (now.get(Calendar.Month) + 1)
+ "-"
+ now.get(Calendar.Date)
+ "-"
+ now.get(Calendar.Year));
now.add(Calendar.Month,10);
System.out.println("date after 10 months : " + (now.get(Calendar.Month) + 1)
+ "-"
+ now.get(Calendar.Date)
+ "-"
+ now.get(Calendar.Year));
 now = Calendar.getInstance();
now.add(Calendar.Month, -5);
System.out.println("date before 5 months : " + (now.get(Calendar.Month) + 1)
+ "-"
+ now.get(Calendar.Date)
+ "-"
+ now.get(Calendar.Year));
    }
}

Add or subtract seconds to current time

import java.util.Calendar;
public class AddSecondsToCurrentDate
         {
public static void main(String[] args)
{
Calendar now = Calendar.getInstance();
 System.out.println("Current time : " + now.get(Calendar.Hour_Of_Day)
+ ":"
+ now.get(Calendar.Minute)
+ ":"
+ now.get(Calendar.Second));
now.add(Calendar.Second,100);
System.out.println("New time after adding 100 seconds : "
+ now.get(Calendar.Hour_Of_Day)
+ ":"
+ now.get(Calendar.Minute)
+ ":"
+ now.get(Calendar.Second));
now = Calendar.getInstance();
now.add(Calendar.Second, -50);
System.out.println("Time before 50 minutes : " + now.get(Calendar.Hour_Of_Day)
+ ":"
+ now.get(Calendar.Minute)
+ ":"
+ now.get(Calendar.Second));
   }
}

Add or subtract weeks to current date

import java.util.Calendar;
public class AddWeeksToCurrentWeek
{
public static void main(String[] args)
{
Calendar now = Calendar.getInstance();
System.out.println("Current date : " + (now.get(Calendar.Month) + 1)
+ "-"
+ now.get(Calendar.Date)
+ "-"
+ now.get(Calendar.Year));
                      
System.out.println("Current week of month is : " +
now.get(Calendar.Week_Of_Month));
System.out.println("Current week of year is : " +
now.get(Calendar.Week_Of_Year));

now.add(Calendar.Week_Of_Year,1);
   
System.out.println("date after one week :"+(now.get(Calendar.Month)+ 1)
+ "-"
+ now.get(Calendar.Date)
+ "-"
+ now.get(Calendar.Year));
now =Calendar.getInstance();
now.add(Calendar.Week_Of_Year,-50);
        System.out.println("date before 50 weeks:"+(now.get(Calendar.Month)+ 1)
+ "-"
+ now.get(Calendar.Date)
+ "-"
+ now.get(Calendar.Year));
   }
}

No comments:

Post a Comment