Java Today isToday(String sdate)

Here you can find the source of isToday(String sdate)

Description

is Today

License

Open Source License

Declaration

public static boolean isToday(String sdate) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.text.ParseException;
import java.text.SimpleDateFormat;

import java.util.Date;

public class Main {
    private final static ThreadLocal<SimpleDateFormat> DATE_FORMATER = new ThreadLocal<SimpleDateFormat>() {
        @Override//from   w w  w  .  ja  v a  2 s.  c  o  m
        protected SimpleDateFormat initialValue() {
            return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        }
    };
    private final static ThreadLocal<SimpleDateFormat> DATE_FORMATER_2 = new ThreadLocal<SimpleDateFormat>() {
        @Override
        protected SimpleDateFormat initialValue() {
            return new SimpleDateFormat("yyyy-MM-dd");
        }
    };

    public static boolean isToday(String sdate) {
        boolean b = false;
        Date time = toDate(sdate);
        Date today = new Date();
        if (time != null) {
            String nowDate = DATE_FORMATER_2.get().format(today);
            String timeDate = DATE_FORMATER_2.get().format(time);
            if (nowDate.equals(timeDate)) {
                b = true;
            }
        }
        return b;
    }

    public static Date toDate(String sdate) {
        return toDate(sdate, DATE_FORMATER.get());
    }

    public static Date toDate(String sdate, SimpleDateFormat dateFormater) {
        try {
            return dateFormater.parse(sdate);
        } catch (ParseException e) {
            return null;
        }
    }
}

Related

  1. isToDay(long millis)
  2. isToday(long milliseconds)
  3. isToday(long timestamp)
  4. isToday(String depDateTime)
  5. isToday(String sdate)
  6. parseToday(String s)
  7. plusDaysToday(int days)
  8. sameTimeToday(Date time)
  9. startOfToday()