Date Operation in java
There are two essential factor in any programing language that is DATE and TIME.I am trying to cover all date/time operation that is really essential for implementing many concepts at real-time. I am just covering the following function for the date operation, rest will i cover in my next blog.
Java is also provides a very special class – DATE .It is contains inside the java.util package.Now i am describing it to step by step to get better understanding in DATE operation.
Returns current date for the current Locale
Long representing the current date of the current Locale
String representing the current Date of the current Locale
Prints name of the month
Meduim date for the locale
Suppose if you have a requirement to show the locale date format to the user.
Means if user is access your application from any other country so date format should be locale for that particular area.
Locale format
Locale.ENGLISH Apr 8, 2011
Locale.CHINA 2011-4-8
Locale.CHINA 08.04.2011
Short date for the locale
Long date for the locale
Full date for the locale
Current date in pattern given by pattern
Get date in MM/dd/yyyy format
Get string representation of a date in pattern given by pattern
Get current year
Get year form the date
Get Next Year
Get previous year
Get current month
Get next month of the current month
Get next month of the month in the date
Get date form String with given pattern and date in string
Get year is leap year or not
Get year represented by date is leap or not
If you have any question so please free feel to ask.
There are two essential factor in any programing language that is DATE and TIME.I am trying to cover all date/time operation that is really essential for implementing many concepts at real-time. I am just covering the following function for the date operation, rest will i cover in my next blog.
Returns current date for the current Locale
Long representing the current date of the current Locale
String representing the current Date of the current Locale
Prints name of the month
Medium date for the locale
Short date for the locale
Long date for the locale
Full date for the locale
Get year represented by date is leap or not
Get year is leap year or not
Get date form String with given pattern and date in string
Get next month of the month in the date
Get next month of the current month
Get current month
Get previous year
Get Next Year
Get year form the date
Get current year
Get string representation of a date in pattern given by pattern
Get date in MM/dd/yyyy format
Current date in pattern given by pattern
Java is also provides a very special class – DATE .It is contains inside the java.util package.Now i am describing it to step by step to get better understanding in DATE operation.
Returns current date for the current Locale
import
java.text.DateFormat;
import
java.util.Date;
import
java.util.Locale;
public
class
AOIDateOperation {
public
Date getCurrentDate() {
return
new
Date();
}
public
static
void
main(String[] args) {
AOIDateOperation aoiDate =
new
AOIDateOperation();
Date currentDateLocale = aoiDate.getCurrentDate();
System.out.println(
"Current date for the current Locale "
+currentDateLocale);
}
}
import
java.text.DateFormat;
import
java.util.Date;
import
java.util.Locale;
public
class
AOIDateOperation {
public
long
getCurrentDateAsLong() {
return
new
Date().getTime();
}
public
static
void
main(String[] args) {
AOIDateOperation aoiDate =
new
AOIDateOperation();
long
dateInLong = aoiDate.getCurrentDateAsLong();
System.out.println(
"Long representing the current date of the current Locale "
+dateInLong);
}
}
String representing the current Date of the current Locale
public
class
AOIDateOperation {
public
String getCurrentDateAsString() {
return
new
Date().toString();
}
public
static
void
main(String[] args) {
AOIDateOperation aoiDate =
new
AOIDateOperation();
String dateInString = aoiDate.getCurrentDateAsString();
System.out.println(
"String representing the current date of the current Locale "
+dateInString);
}
}
import
java.text.DateFormat;
import
java.util.Date;
import
java.util.Locale;
public
class
AOIDateOperation {
public
String printMonth(
int
monthIndex) {
final
String[] months = {
"January"
,
"February"
,
"March"
,
"April"
,
"May"
,
"June"
,
"July"
,
"August"
,
"September"
,
"October"
,
"November"
,
"December"
};
return
months[monthIndex-
1
];
}
public
static
void
main(String[] args) {
AOIDateOperation aoiDate =
new
AOIDateOperation();
String nameOftheMonth = aoiDate.printMonth(
1
);
System.out.println(
"Prints name of the month "
+nameOftheMonth);
}
}
Suppose if you have a requirement to show the locale date format to the user.
Means if user is access your application from any other country so date format should be locale for that particular area.
Locale format
Locale.ENGLISH Apr 8, 2011
Locale.CHINA 2011-4-8
Locale.CHINA 08.04.2011
import
java.text.DateFormat;
import
java.util.Date;
import
java.util.Locale;
public
class
AOIDateOperation {
public
String getMediumDateByLocale(Locale locale) {
return
DateFormat.getDateInstance(DateFormat.MEDIUM, locale).format(
new
Date());
}
public
static
void
main(String[] args) {
AOIDateOperation aoiDate =
new
AOIDateOperation();
String mediumDate = aoiDate.getMediumDateByLocale(Locale.ENGLISH);
System.out.println(
"Meduim date for the locale "
+mediumDate);
}
}
import
java.text.DateFormat;
import
java.util.Date;
import
java.util.Locale;
public
class
AOIDateOperation {
/**
* This methods gives short date for the locale
* @param locale a Locale
* @return String on success,null otherwise
*/
public
 String getShortDateByLocale(Locale locale) {
return
DateFormat.getDateInstance(DateFormat.SHORT, locale).format(
new
Date());
}
public
static
void
main(String[] args) {
AOIDateOperation aoiDate =
new
AOIDateOperation();
String shortDate=aoiDate.getShortDateByLocale(Locale.ENGLISH);
System.out.println(
"short date for the locale "
+shortDate);
}
}
import
java.text.DateFormat;
import
java.util.Date;
import
java.util.Locale;
public
class
AOIDateOperation {
public
String getLongDateByLocale(Locale locale) {
return
DateFormat.getDateInstance(DateFormat.LONG, locale).format(
new
Date());
}
public
static
void
main(String[] args) {
AOIDateOperation aoiDate =
new
AOIDateOperation();
String longDate=aoiDate.getLongDateByLocale(Locale.ENGLISH);
System.out.println(
"Long date for the locale "
+longDate);
}
}
import
java.text.DateFormat;
import
java.util.Date;
import
java.util.Locale;
public
class
AOIDateOperation {
public
String getFullDateByLocale(Locale locale) {
return
DateFormat.getDateInstance(DateFormat.FULL, locale).format(
new
Date());
}
public
static
void
main(String[] args) {
AOIDateOperation aoiDate =
new
AOIDateOperation();
String fullDate=aoiDate.getFullDateByLocale(Locale.ENGLISH);
System.out.println(
"Full date for the locale "
+fullDate);
}
}
import
java.text.SimpleDateFormat;
import
java.util.Date;
public
class
AOIDateOperation {
public
String getCurrentDateByPattern(String pattern) {
return
new
SimpleDateFormat(pattern).format(
new
Date());
}
public
static
void
main(String[] args) {
AOIDateOperation aoiDate =
new
AOIDateOperation();
String patternDate=aoiDate.getCurrentDateByPattern(
"MM/dd/yyyy"
);
System.out.println(
"Current date in pattern given by pattern "
+patternDate);
}
}
import
java.text.SimpleDateFormat;
import
java.util.Date;
public
class
AOIDateOperation {
public
String getDateInMMDDYYYY(Date date) {
return
new
SimpleDateFormat(
"MM/dd/yyyy"
).format(date);
}
public
static
void
main(String[] args) {
AOIDateOperation aoiDate =
new
AOIDateOperation();
String patternDate=aoiDate.getDateInMMDDYYYY(
new
Date());
System.out.println(
"Gives date in MM/dd/yyyy format"
+patternDate);
}
}
Get string representation of a date in pattern given by pattern
import
java.text.SimpleDateFormat;
import
java.util.Date;
public
class
AOIDateOperation {
public
String getDateToStringByPattern(String pattern, Date date) {
return
new
SimpleDateFormat(pattern).format(date);
}
public
static
void
main(String[] args) {
AOIDateOperation aoiDate =
new
AOIDateOperation();
String patternStringDate=aoiDate.getDateToStringByPattern(
"MM/dd/yyyy"
,
new
Date());
System.out.println(
"string representation of a date in pattern given by pattern "
+patternStringDate);
}
}
Get current year
import
java.util.Calendar;
import
java.util.GregorianCalendar;
public
class
AOIDateOperation {
public
Integer getYear() {
return
new
GregorianCalendar().get(Calendar.YEAR);
}
public
static
void
main(String[] args) {
AOIDateOperation aoiDate =
new
AOIDateOperation();
Integer currentYear=aoiDate.getYear();
System.out.println(
"string representation of a date in pattern given by pattern "
+currentYear);
}
}
Get year form the date
import
java.util.Calendar;
import
java.util.Date;
import
java.util.GregorianCalendar;
public
class
AOIDateOperation {
public
Integer getYear(Date date) {
GregorianCalendar gCal =
new
GregorianCalendar();
gCal.setTime(date);
return
gCal.get(Calendar.YEAR);
}
public
static
void
main(String[] args) {
AOIDateOperation aoiDate =
new
AOIDateOperation();
Integer currentYearFromGivenDate=aoiDate.getYear(
new
Date());
System.out.println(
"gives year form the date "
+currentYearFromGivenDate);
}
}
import
java.util.Calendar;
import
java.util.GregorianCalendar;
public
class
AOIDateOperation {
public
Integer getNextYear() {
return
new
GregorianCalendar().get(Calendar.YEAR)+
1
;
}
public
static
void
main(String[] args) {
AOIDateOperation aoiDate =
new
AOIDateOperation();
Integer nextYear=aoiDate.getNextYear();
System.out.println(
"Returns next year "
+nextYear);
}
}
import
java.util.Calendar;
import
java.util.GregorianCalendar;
public
class
AOIDateOperation {
public
Integer getPreviousYear() {
return
new
GregorianCalendar().get(Calendar.YEAR)-
1
;
}
public
static
void
main(String[] args) {
AOIDateOperation aoiDate =
new
AOIDateOperation();
Integer previousYear=aoiDate.getPreviousYear();
System.out.println(
"Returns previous year "
+previousYear);
}
}
import
java.util.Calendar;
import
java.util.GregorianCalendar;
public
class
AOIDateOperation {
public
Integer getMonth() {
return
new
GregorianCalendar().get(Calendar.MONTH)+
1
;
}
public
static
void
main(String[] args) {
AOIDateOperation aoiDate =
new
AOIDateOperation();
Integer month=aoiDate.getMonth();
System.out.println(
"Returns current Month "
+month);
}
}
public
class
AOIDateOperation {
public
Integer getNextMonth() {
return
new
GregorianCalendar().get(Calendar.MONTH) == Calendar.DECEMBER ?
1
:
new
GregorianCalendar().get(Calendar.MONTH) +
2
;
}
public
static
void
main(String[] args) {
AOIDateOperation aoiDate =
new
AOIDateOperation();
Integer nextMonth=aoiDate.getNextMonth();
System.out.println(
"Returns Next Month "
+nextMonth);
}
}
public
class
AOIDateOperation {
public
Integer getNextMonth(Date date) {
GregorianCalendar cal =
new
GregorianCalendar();
cal.setTime(date);
return
cal.get(Calendar.MONTH) == Calendar.DECEMBER ?
1
: cal.get(Calendar.MONTH) +
2
;
}
public
static
void
main(String[] args) {
AOIDateOperation aoiDate =
new
AOIDateOperation();
Integer nextMonth=aoiDate.getNextMonth(
new
Date());
System.out.println(
"Returns Next to Next Month "
+nextMonth);
}
}
Get date form String with given pattern and date in string
import
java.text.ParseException;
import
java.text.SimpleDateFormat;
import
java.util.Calendar;
import
java.util.Date;
import
java.util.GregorianCalendar;
public
class
AOIDateOperation {
public
 Integer getNextMonth(String pattern, String dateStr) {
return
getNextMonth(convertStringToDate(pattern, dateStr));
}
public
static
void
main(String[] args) {
AOIDateOperation aoiDate =
new
AOIDateOperation();
Integer nextYear=aoiDate.getNextMonth(
"MM/dd/yyyy"
,
"12/01/2010"
);
System.out.println(
"Returns date form String with given pattern and date in string "
+nextYear);
}
public
static
Date convertStringToDate(String pattern, String dateStr) {
try
{
return
new
SimpleDateFormat(pattern).parse(dateStr);
}
catch
(ParseException e) {
}
return
null
;
}
public
Integer getNextMonth(Date date) {
GregorianCalendar cal =
new
GregorianCalendar();
cal.setTime(date);
return
cal.get(Calendar.MONTH) == Calendar.DECEMBER ?
1
: cal.get(Calendar.MONTH) +
2
;
}
}
Get year is leap year or not
import
java.util.GregorianCalendar;
public
class
AOIDateOperation {
public
boolean
isLeapYear(
int
year) {
return
new
GregorianCalendar().isLeapYear(year);
}
public
static
void
main(String[] args) {
AOIDateOperation aoiDate =
new
AOIDateOperation();
boolean
leapYear = aoiDate.isLeapYear(
2010
);
System.out.println(
"Is Leap Year ??? "
+ leapYear);
}
}
Get year represented by date is leap or not
import
java.util.Calendar;
import
java.util.Date;
import
java.util.GregorianCalendar;
public
class
AOIDateOperation {
public
Â
boolean
isLeapYear(Date date) {
return
isLeapYear(getYear(date));
}
public
static
void
main(String[] args) {
AOIDateOperation aoiDate =
new
AOIDateOperation();
boolean
leapYear = aoiDate.isLeapYear(
new
Date());
System.out.println(
"Is Leap Year"
+ leapYear);
}
public
Integer getYear(Date date) {
GregorianCalendar gCal =
new
GregorianCalendar();
gCal.setTime(date);
return
gCal.get(Calendar.YEAR);
}
public
boolean
isLeapYear(
int
year) {
return
new
GregorianCalendar().isLeapYear(year);
}
}
No comments:
Post a Comment