Blog coding and discussion of coding about JavaScript, PHP, CGI, general web building etc.

Friday, October 28, 2016

Check date with todays date

Check date with todays date


I have written some code to check two dates, a start date and an end date. If the end date is before the start date, it will give a prompt that says End date is before start date.

I also want to add a check for if the start date is before today (today as in the day of which the user uses the application) How would I do this? ( Date checker code below, also all this is written for android if that has any bearing)

     if(startYear > endYear)              {                  fill = fill + 1;                  message = message + "End Date is Before Start Date" + "\n";              }          else if(startMonth > endMonth && startYear >= endYear)                  {                      fill = fill + 1;                      message = message + "End Date is Before Start Date" + "\n";                  }          else if(startDay > endDay && startMonth >= endMonth && startYear >= endYear)                      {                          fill = fill + 1;                          message = message + "End Date is Before Start Date" + "\n";                      }  

Answer by sudocode for Check date with todays date


Does this help?

Calendar c = Calendar.getInstance();    // set the calendar to start of today  c.set(Calendar.HOUR_OF_DAY, 0);  c.set(Calendar.MINUTE, 0);  c.set(Calendar.SECOND, 0);  c.set(Calendar.MILLISECOND, 0);    // and get that as a Date  Date today = c.getTime();    // or as a timestamp in milliseconds  long todayInMillis = c.getTimeInMillis();    // user-specified date which you are testing  // let's say the components come from a form or something  int year = 2011;  int month = 5;  int dayOfMonth = 20;    // reuse the calendar to set user specified date  c.set(Calendar.YEAR, year);  c.set(Calendar.MONTH, month);  c.set(Calendar.DAY_OF_MONTH, dayOfMonth);    // and get that as a Date  Date dateSpecified = c.getTime();    // test your condition  if (dateSpecified.before(today)) {    System.err.println("Date specified [" + dateSpecified + "] is before today [" + today + "]");  } else {    System.err.println("Date specified [" + dateSpecified + "] is NOT before today [" + today + "]");  }  

Answer by Mitch Salopek for Check date with todays date


I assume you are using integers to represent your year, month, and day? If you want to remain consistent, use the Date methods.

Calendar cal = new Calendar();  int currentYear, currentMonth, currentDay;   currentYear = cal.get(Calendar.YEAR);   currentMonth = cal.get(Calendar.MONTH);   currentDay = cal.get(Calendar.DAY_OF_WEEK);         if(startYear < currentYear)                  {                      message = message + "Start Date is Before Today" + "\n";                  }              else if(startMonth < currentMonth && startYear <= currentYear)                      {                          message = message + "Start Date is Before Today" + "\n";                      }              else if(startDay < currentDay && startMonth <= currentMonth && startYear <= currentYear)                          {                              message = message + "Start Date is Before Today" + "\n";                          }  

Answer by martin for Check date with todays date


Using Joda Time this can be simplified to:

DateMidnight startDate = new DateMidnight(startYear, startMonth, startDay);  if (startDate.isBeforeNow())  {      // startDate is before now      // do something...  }  

Answer by Milan Shukla for Check date with todays date


to check if a date is today's date or not only check for dates not time included with that so make time 00:00:00 and use the code below

    Calendar c = Calendar.getInstance();        // set the calendar to start of today      c.set(Calendar.HOUR_OF_DAY, 0);      c.set(Calendar.MINUTE, 0);      c.set(Calendar.SECOND, 0);      c.set(Calendar.MILLISECOND, 0);        Date today = c.getTime();        // or as a timestamp in milliseconds      long todayInMillis = c.getTimeInMillis();          int dayOfMonth = 24;      int month = 4;      int year =2013;        // reuse the calendar to set user specified date      c.set(Calendar.YEAR, year);      c.set(Calendar.MONTH, month - 1);      c.set(Calendar.DAY_OF_MONTH, dayOfMonth);      c.set(Calendar.HOUR_OF_DAY, 0);      c.set(Calendar.MINUTE, 0);      c.set(Calendar.SECOND, 0);      c.set(Calendar.MILLISECOND, 0);      // and get that as a Date      Date dateSpecified = c.getTime();        // test your condition      if (dateSpecified.before(today)) {            Log.v(" date is previou")      } else if (dateSpecified.equal(today)) {            Log.v(" date is today ")      }                else if (dateSpecified.after(today)) {            Log.v(" date is future date ")      }   

Hope it will help....

Answer by Vladimir.Shramov for Check date with todays date


    boolean isBeforeToday(Date d) {          Date today = new Date();          today.setHours(0);          today.setMinutes(0);          today.setSeconds(0);          return d.before(today);      }  

Answer by Kishath for Check date with todays date


Don't complicate it that much. Use this easy way. Import DateUtils java class and call the following methods which returns a boolean.

DateUtils.isSameDay(date1,date2);  DateUtils.isSameDay(calender1,calender2);  DateUtils.isToday(date1);  

For more info refer this article DateUtils Java

Answer by carlol for Check date with todays date


another way to do this operation:

public class TimeUtils {        /**       * @param timestamp       * @return       */      public static boolean isToday(long timestamp) {          Calendar now = Calendar.getInstance();          Calendar timeToCheck = Calendar.getInstance();          timeToCheck.setTimeInMillis(timestamp);          return (now.get(Calendar.YEAR) == timeToCheck.get(Calendar.YEAR)                  && now.get(Calendar.DAY_OF_YEAR) == timeToCheck.get(Calendar.DAY_OF_YEAR));      }    }  

Answer by NoFuchsGiven for Check date with todays date


Try this:

public static boolean isToday(Date date)  {      return org.apache.commons.lang3.time.DateUtils.isSameDay(Calendar.getInstance().getTime(),date);  }  

Answer by Basil Bourque for Check date with todays date


The other answers ignore the crucial issue of time zone.

The other answers use outmoded classes.

Avoid old date-time classes

The old date-time classes bundled with the earliest versions of Java are poorly designed, confusing, and troublesome. Avoid java.util.Date/.Calendar and related classes.

java.time

LocalDate

For date-only values, without time-of-day and without time zone, use the LocalDate class.

LocalDate start = LocalDate.of( 2016 , 1 , 1 );  LocalDate stop = start.plusWeeks( 1 );  

Time Zone

Be aware that while LocalDate does not store a time zone, determining a date such as ?today? requires a time zone. For any given moment, the date may vary around the world by time zone. For example, a new day dawns earlier in Paris than in Montréal. A moment after midnight in Paris is still ?yesterday? in Montréal.

If all you have is an offset-from-UTC, use ZoneOffset. If you have a full time zone (continent/region), then use ZoneId. If you want UTC, use the handy constant ZoneOffset.UTC.

ZoneId zoneId = ZoneId.of( "America/Montreal" );  LocalDate today = LocalDate.now( zoneId );  

Comparing is easy with isEqual, isBefore, and isAfter methods.

boolean invalidInterval = stop.isBefore( start );  

We can check to see if today is contained within this date range. In my logic shown here I use the Half-Open approach where the beginning is inclusive while the ending is exclusive. This approach is common in date-time work. So, for example, a week runs from a Monday going up to but not including the following Monday.

// Is today equal or after start (not before) AND today is before stop.  boolean intervalContainsToday = ( ! today.isBefore( start ) ) && today.isBefore( stop ) ) ;  

Interval

If working extensively with such spans of time, consider adding the ThreeTen-Extra library to your project. This library extends the java.time framework, and is the proving ground for possible additions to java.time.

ThreeTen-Extra includes an Interval class with handy methods such as abuts, contains, encloses, overlaps, and so on.


Fatal error: Call to a member function getElementsByTagName() on a non-object in D:\XAMPP INSTALLASTION\xampp\htdocs\endunpratama9i\www-stackoverflow-info-proses.php on line 72

0 comments:

Post a Comment

Popular Posts

Powered by Blogger.