Monday, November 3, 2014

Java month year pro

Get Week of month and year

import java.util.Calendar;
public class GetWeekOfMonthAndYear
{
public static void main(String[] args)
 {
Calendar now = Calendar.getInstance();
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_Month, 1);
System.out.println("date after one year :"+(now.get(Calendar.Month) + 1)
+ "-"
+ now.get(Calendar.Date)
+ "-"
+ now.get(Calendar.Year));
  }
}

Get current Time Zone

import java.util.Calendar;
import java.util.TimeZone;
public class GetCurrentTimeZone
{
public static void main(String[] args)
{
Calendar now = Calendar.getInstance();
TimeZone timeZone = now.getTimeZone();
class
System.out.println("Current TimeZone is : " + timeZone.getDisplayName());
   }
}

Get current date time values

import java.util.Calendar;
public class GetCurrentDateTimeExample
{
public static void main(String[] args)
{
Calendar now = Calendar.getInstance();
System.out.println("Current Year is : " + now.get(Calendar.Year));
System.out.println("Current Month is:"+(now.get(Calendar.Month) + 1 ));
System.out.println("Current Date is : " + now.get(Calendar.Date));
System.out.println("Current Hour in 12 hour format is : "                        
+ now.get(Calendar.HOUR));
System.out.println("Current Hour in 24 hour format is : "
+ now.get(Calendar.Hour_Of_Day));
System.out.println("Current Minute is : " + now.get(Calendar.Minute));
System.out.println("Current Second is : " + now.get(Calendar.Second));
System.out.println("Current Millisecond is : " + now.get(Calendar.Millisecond));
System.out.println("Current full date time is : " +
(now.get(Calendar.Month) + 1)
+ "-"
+ now.get(Calendar.Date)
+ "-"
+ now.get(Calendar.Year)
+ " "
+ now.get(Calendar.Hour_Of_Day)
+ ":"
+ now.get(Calendar.Minute)
+ ":"
+ now.get(Calendar.Second)
+ "."
+ now.get(Calendar.Millisecond)
  );        
  }
}

Get time in milliseconds

import java.util.Calendar;
public class GetTimeInMilliSeconds
{
 public static void main(String[] args)
{
Calendar now = Calendar.getInstance();
System.out.println("Current milliseconds since Jan 1, 1970 are :"
+ now.getTimeInMillis());
   }
}

Simple Java Calendar

import java.util.Calendar;
public class JavaSimpleCalendarExample
 {
public static void main(String[] args)
{
Calendar cal = Calendar.getInstance();
System.out.println("Today is : " + cal.getTime());  
  }
}


No comments:

Post a Comment