Wednesday, 6 August 2014

To get Start and End date by given Month, Year and Day

import java.util.Calendar;
import java.util.Date;

public class HelloWorld {

public static void main(String[] args) {

Date fromDate = getFromDate(2014, 7, null);
System.out.println(" From Date is " + fromDate);

fromDate = getFromDate(2014, 7, 14);
System.out.println(" From Date is " + fromDate);

Date toDate = getToDate(2014, 7, null);
System.out.println(" To Date is " + toDate);

toDate = getToDate(2014, 7, 14);
System.out.println(" To Date is " + toDate);

}

private static Date getFromDate(Integer year, Integer month, Integer day) {
// Setup a Calendar instance.
Calendar cal = Calendar.getInstance();
cal.setLenient(false);
// Set the year as 2009
cal.set(Calendar.YEAR, year);
// Set the month as February (can be set as 1 or Calendar.FEBRUARY)
cal.set(Calendar.MONTH, month);
// Set the date as 1st - optional
if (day == null)
cal.set(Calendar.DATE, 1);
else
cal.set(Calendar.DAY_OF_MONTH, day);
cal.set(Calendar.HOUR_OF_DAY, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
return cal.getTime();
}

private static Date getToDate(Integer year, Integer month, Integer day) {
// Setup a Calendar instance.
Calendar cal = Calendar.getInstance();
cal.setLenient(false);
// Set the year as 2009
cal.set(Calendar.YEAR, year);
// Set the month as February (can be set as 1 or Calendar.FEBRUARY)
cal.set(Calendar.MONTH, month - 1);
// Set the date as 1st - optional

if (day == null) {
int lastDateOfMonth = cal.getActualMaximum(Calendar.DATE);
cal.set(Calendar.DATE, lastDateOfMonth);
} else
cal.set(Calendar.DAY_OF_MONTH, day);
cal.set(Calendar.HOUR_OF_DAY, 23);
cal.set(Calendar.MINUTE, 59);
cal.set(Calendar.SECOND, 59);
return cal.getTime();
}
}

Output:


From Date is  Fri Aug 01 00:00:00 IST 2014
From Date is  Thu Aug 14 00:00:00 IST 2014
To Date is  Thu Jul 31 23:59:59 IST 2014
To Date is  Mon Jul 14 23:59:59 IST 2014

2 comments:

  1. https://www.eswasthyaseva.com/singleBlog.php?id=NDI1
    A new study from Lund University in Sweden has opened the way for new approaches to slowing the development of AIDS in HIV-1-infected patients. It is hoped that this could lead to better treatment methods and preventive measures to combat HIV and AIDS.The findings have just been published in the distinguished scientific journal New England Journal of Medicine.
    The most common type of the virus that causes AIDS -- HIV-1 -- is less aggressive when it infects a person already carrying the milder HIV-2. The study looked at how the disease developed in those who had been infected with HIV-1 and those who were infected with both HIV-1 and HIV-2."The moderating effect of HIV-2 was extremely strong. The time it took to develop AIDS was around 50 per cent longer for those infected with both strains than for those only carrying the HIV-1 virus. The unusually large difference makes me, as a researcher, very optimistic that it will be possible to identify new and significant approaches that can be taken to combating the development of AIDS," says Joakim Esbjörnsson, a virologist at Lund University.

    ReplyDelete
  2. Web Development company in Noida, website builder & agency, SEO Services and offering E-commerce & responsive website development Solutions.
    http://www.99professionals.com/

    ReplyDelete