I have tried to cover the all month operational handle in java, that
is really helpful for any real life development application.
Get the name of the Next Month :
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
public class AOIDateOperation {
public Integer getNextMonth() {
return new GregorianCalendar().get(Calendar.MONTH) == Calendar.DECEMBER ? 1 : new GregorianCalendar().get(Calendar.MONTH) + 2;
}
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 String getNameOfNextMonth(Date date) {
return printMonth(getNextMonth());
}
public static void main(String[] args) {
AOIDateOperation aoiDate = new AOIDateOperation();
String nextMonth = aoiDate.getNameOfNextMonth(new Date());
System.out.println("Next Month is " + nextMonth);
}
}
This method gives next month
1. pattern String pattern of the Date given in String(dateStr)
2. dateStr String date in string
3. Integer on success, otherwise NULL
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 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 Date convertStringToDate(String pattern, String dateStr) {
try {
return new SimpleDateFormat(pattern).parse(dateStr);
} catch (ParseException e) {
}
return null;
}
public static void main(String[] args) {
AOIDateOperation aoiDate = new AOIDateOperation();
Integer nextMonth = aoiDate.getNextMonth("MM/dd/yyyy","11/04/2011");
System.out.println("Next Month is " + nextMonth);
}
}
This method gives name of the next month
1. pattern String pattern of the Date given in String(dateStr)
2. dateStr String date in string
3. String on success, otherwise NULL
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(Date date) {
GregorianCalendar cal = new GregorianCalendar();
cal.setTime(date);
return cal.get(Calendar.MONTH) == Calendar.DECEMBER ? 1 : cal
.get(Calendar.MONTH) + 2;
}
public Date convertStringToDate(String pattern, String dateStr) {
try {
return new SimpleDateFormat(pattern).parse(dateStr);
} catch (ParseException e) {
}
return null;
}
public String getNextMonthAsString(String pattern, String dateStr) {
return printMonth(getNextMonth(convertStringToDate(pattern, dateStr)));
}
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 nextMonth = aoiDate.getNextMonthAsString("MM/dd/yyyy", "11/04/2011");
System.out.println("Print Month is " + nextMonth);
}
}
This method gives previous month of the current month
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
public class AOIDateOperation {
public Integer getPrivousMonth() {
return getPreviousMonth(new Date());
}
public Integer getPreviousMonth(Date date) {
GregorianCalendar cal = new GregorianCalendar();
cal.setTime(date);
return cal.get(Calendar.MONTH) == Calendar.JANUARY ? Calendar.DECEMBER+1 : cal.get(Calendar.MONTH) ;
}
public static void main(String[] args) {
AOIDateOperation aoiDate = new AOIDateOperation();
Integer previusMonth = aoiDate.getPrivousMonth();
System.out.println("Print Month is " + previusMonth);
}
}
This method gives name of the previous month of current month
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
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 Integer getPrivousMonth() {
return getPreviousMonth(new Date());
}
public String getPrivousMonthAsString() {
return printMonth(getPrivousMonth());
}
public Integer getPreviousMonth(Date date) {
GregorianCalendar cal = new GregorianCalendar();
cal.setTime(date);
return cal.get(Calendar.MONTH) == Calendar.JANUARY ? Calendar.DECEMBER + 1
: cal.get(Calendar.MONTH);
}
public static void main(String[] args) {
AOIDateOperation aoiDate = new AOIDateOperation();
String previusMonth = aoiDate.getPrivousMonthAsString();
System.out.println("Print Month is " + previusMonth);
}
}
This method gives previous month of the date
public Integer getPreviousMonth(Date date) {
GregorianCalendar cal = new GregorianCalendar();
cal.setTime(date);
return cal.get(Calendar.MONTH) == Calendar.JANUARY ? Calendar.DECEMBER+1 : cal.get(Calendar.MONTH) ;
}
This method gives name of the previous month of the month in date
public String getPreviousMonthAsString(Date date) {
return printMonth(getPreviousMonth(date));
}
This method gives previous month of the date presetnted by pattern and dateStr
1. pattern String pattern of the Date given in String(dateStr)
2. dateStr String date in string
3. String on success,null otherwise
public Integer getPreviousMonth(String pattern, String dateStr) {
return getPreviousMonth(convertStringToDate(pattern, dateStr));
}
This method gives name of the previous month of the date presented by pattern and dateStr
1. pattern String pattern of the Date given in String(dateStr)
2. dateStr String date in string
3. String on success, otherwise NULL
public String getPreviousMonthAsString(String pattern, String dateStr) {
return printMonth(getPreviousMonth(convertStringToDate(pattern, dateStr)))
}
Check Validation for Valid Date :
public class DateValidationAOI {
public boolean isValidDate(String date, String format) {
SimpleDateFormat sdf = new SimpleDateFormat(format);
Date testDate = null;
try {
testDate = sdf.parse(date);
} catch (ParseException e) {
return false;
}
if (!sdf.format(testDate).equals(date)) {
return false; }
return true;
}
public static void main(String[] args) {
DateValidationAOI dateValidationAOI = new DateValidationAOI();
boolean unvalidDate = dateValidationAOI.isValidDate("abcd","yyyy-MM-dd");
boolean validDate = dateValidationAOI.isValidDate("2010-10-02","yyyy-MM-dd");
System.out.println("validDate " + validDate +" unvalidDate" +unvalidDate);
}
}
Comparison Between Two Dates
This method compare between two dates if firstDate is greater then secondDate so it will return 1 otherwise -1 and if both dates are same so it will return 0.
Get Before Date in java
public boolean beforeDates(String date) {
return compareDate(new Date(), convertStringToDate(date,"yyyy-MM-dd")) == -1;
}
private int compareDate(Date dateFirstAoi, Date dateSecondAoi) {
GregorianCalendar gregorianCalendar = new GregorianCalendar();
gregorianCalendar.setTime(dateFirst);
GregorianCalendar calToday = new GregorianCalendar();
calToday.setTime(dateSecond);
return cal.compareTo(calToday);
}
Convert String to Date
public Date convertStringToDate(String date,String dateFormat) {
SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
Date d1 = null;
try {
d1 = sdf.parse(date);
} catch (ParseException e) {
e.printStackTrace();
}
return d1;
}
If you have any question regarding to this so please free feel to ask and put your comment in it.
- Get the name of the Next Month
- Get next month in Integer format
- Get integer value of next month based on given parameter like pattern,dateStr
- Get name of next month based on given parameter like pattern,dateStr
- Get integer value of previous month based on given parameter like pattern,dateStr
- Get name of previous month based on given parameter like pattern ,dateStr
Get the name of the Next Month :
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
public class AOIDateOperation {
public Integer getNextMonth() {
return new GregorianCalendar().get(Calendar.MONTH) == Calendar.DECEMBER ? 1 : new GregorianCalendar().get(Calendar.MONTH) + 2;
}
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 String getNameOfNextMonth(Date date) {
return printMonth(getNextMonth());
}
public static void main(String[] args) {
AOIDateOperation aoiDate = new AOIDateOperation();
String nextMonth = aoiDate.getNameOfNextMonth(new Date());
System.out.println("Next Month is " + nextMonth);
}
}
This method gives next month
1. pattern String pattern of the Date given in String(dateStr)
2. dateStr String date in string
3. Integer on success, otherwise NULL
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 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 Date convertStringToDate(String pattern, String dateStr) {
try {
return new SimpleDateFormat(pattern).parse(dateStr);
} catch (ParseException e) {
}
return null;
}
public static void main(String[] args) {
AOIDateOperation aoiDate = new AOIDateOperation();
Integer nextMonth = aoiDate.getNextMonth("MM/dd/yyyy","11/04/2011");
System.out.println("Next Month is " + nextMonth);
}
}
This method gives name of the next month
1. pattern String pattern of the Date given in String(dateStr)
2. dateStr String date in string
3. String on success, otherwise NULL
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(Date date) {
GregorianCalendar cal = new GregorianCalendar();
cal.setTime(date);
return cal.get(Calendar.MONTH) == Calendar.DECEMBER ? 1 : cal
.get(Calendar.MONTH) + 2;
}
public Date convertStringToDate(String pattern, String dateStr) {
try {
return new SimpleDateFormat(pattern).parse(dateStr);
} catch (ParseException e) {
}
return null;
}
public String getNextMonthAsString(String pattern, String dateStr) {
return printMonth(getNextMonth(convertStringToDate(pattern, dateStr)));
}
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 nextMonth = aoiDate.getNextMonthAsString("MM/dd/yyyy", "11/04/2011");
System.out.println("Print Month is " + nextMonth);
}
}
This method gives previous month of the current month
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
public class AOIDateOperation {
public Integer getPrivousMonth() {
return getPreviousMonth(new Date());
}
public Integer getPreviousMonth(Date date) {
GregorianCalendar cal = new GregorianCalendar();
cal.setTime(date);
return cal.get(Calendar.MONTH) == Calendar.JANUARY ? Calendar.DECEMBER+1 : cal.get(Calendar.MONTH) ;
}
public static void main(String[] args) {
AOIDateOperation aoiDate = new AOIDateOperation();
Integer previusMonth = aoiDate.getPrivousMonth();
System.out.println("Print Month is " + previusMonth);
}
}
This method gives name of the previous month of current month
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
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 Integer getPrivousMonth() {
return getPreviousMonth(new Date());
}
public String getPrivousMonthAsString() {
return printMonth(getPrivousMonth());
}
public Integer getPreviousMonth(Date date) {
GregorianCalendar cal = new GregorianCalendar();
cal.setTime(date);
return cal.get(Calendar.MONTH) == Calendar.JANUARY ? Calendar.DECEMBER + 1
: cal.get(Calendar.MONTH);
}
public static void main(String[] args) {
AOIDateOperation aoiDate = new AOIDateOperation();
String previusMonth = aoiDate.getPrivousMonthAsString();
System.out.println("Print Month is " + previusMonth);
}
}
This method gives previous month of the date
public Integer getPreviousMonth(Date date) {
GregorianCalendar cal = new GregorianCalendar();
cal.setTime(date);
return cal.get(Calendar.MONTH) == Calendar.JANUARY ? Calendar.DECEMBER+1 : cal.get(Calendar.MONTH) ;
}
This method gives name of the previous month of the month in date
public String getPreviousMonthAsString(Date date) {
return printMonth(getPreviousMonth(date));
}
This method gives previous month of the date presetnted by pattern and dateStr
1. pattern String pattern of the Date given in String(dateStr)
2. dateStr String date in string
3. String on success,null otherwise
public Integer getPreviousMonth(String pattern, String dateStr) {
return getPreviousMonth(convertStringToDate(pattern, dateStr));
}
This method gives name of the previous month of the date presented by pattern and dateStr
1. pattern String pattern of the Date given in String(dateStr)
2. dateStr String date in string
3. String on success, otherwise NULL
public String getPreviousMonthAsString(String pattern, String dateStr) {
return printMonth(getPreviousMonth(convertStringToDate(pattern, dateStr)))
}
Check Validation for Valid Date :
public class DateValidationAOI {
public boolean isValidDate(String date, String format) {
SimpleDateFormat sdf = new SimpleDateFormat(format);
Date testDate = null;
try {
testDate = sdf.parse(date);
} catch (ParseException e) {
return false;
}
if (!sdf.format(testDate).equals(date)) {
return false; }
return true;
}
public static void main(String[] args) {
DateValidationAOI dateValidationAOI = new DateValidationAOI();
boolean unvalidDate = dateValidationAOI.isValidDate("abcd","yyyy-MM-dd");
boolean validDate = dateValidationAOI.isValidDate("2010-10-02","yyyy-MM-dd");
System.out.println("validDate " + validDate +" unvalidDate" +unvalidDate);
}
}
Comparison Between Two Dates
This method compare between two dates if firstDate is greater then secondDate so it will return 1 otherwise -1 and if both dates are same so it will return 0.
private
int
compareDate(Date dateFirstAoi, Date dateSecondAoi) {
GregorianCalendar gregorianCalendar =
new
GregorianCalendar();
gregorianCalendar.setTime(dateFirst);
GregorianCalendar calToday =
new
GregorianCalendar();
calToday.setTime(dateSecond);
return
cal.compareTo(calToday);
}
public boolean beforeDates(String date) {
return compareDate(new Date(), convertStringToDate(date,"yyyy-MM-dd")) == -1;
}
private int compareDate(Date dateFirstAoi, Date dateSecondAoi) {
GregorianCalendar gregorianCalendar = new GregorianCalendar();
gregorianCalendar.setTime(dateFirst);
GregorianCalendar calToday = new GregorianCalendar();
calToday.setTime(dateSecond);
return cal.compareTo(calToday);
}
Convert String to Date
public Date convertStringToDate(String date,String dateFormat) {
SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
Date d1 = null;
try {
d1 = sdf.parse(date);
} catch (ParseException e) {
e.printStackTrace();
}
return d1;
}
If you have any question regarding to this so please free feel to ask and put your comment in it.
No comments:
Post a Comment