欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

java判断今天,昨天,前天,不能用秒间隔的简单实例

程序员文章站 2024-03-05 11:41:06
实例如下: 一 json optstring 解析的timestamp string 二 long datesec = (long) (double.val...

实例如下:

一 json optstring 解析的timestamp string

二 long datesec = (long) (double.valueof(timestamp) * 1000);

三 date date=new date(datesec);

四 gettimestampstring(date);

public static string gettimestampstring(date paramdate) {
    string str = null;
    long l = paramdate.gettime();
    calendar localcalendar = gregoriancalendar.getinstance();
    localcalendar.settime(paramdate);
    int year = localcalendar.get(calendar.year);
    if(! issameyear(year)){ //去年,直接返回
      string paramdate2str = new simpledateformat("yyyy mm dd", locale.china).format(paramdate);
      return paramdate2str;
    }


    if (issameday(l)) {
      int i = localcalendar.get(calendar.hour_of_day);
      if (i > 17) {
        str = "晚上 hh:mm";//hh表示24小时,hh表示12小时进制,
      } else if ((i >= 0) && (i <= 6)) {
        str = "凌晨 hh:mm";
      } else if ((i > 11) && (i <= 17)) {
        str = "下午 hh:mm";
      } else {
        str = "上午 hh:mm";
      }
    } else if (isyesterday(l)) {
      str = "昨天 hh:mm";
    } else if (isbeforeyesterday(l)) {
      str = "前天 hh:mm";
    } else {
      str = "m月d日 hh:mm";
    }
    string paramdate2str = new simpledateformat(str, locale.china).format(paramdate);
    return paramdate2str;
  }

。。。

//  获取 今天开始结束 时间

 public static timeinfo gettodaystartandendtime() {

    calendar localcalendar1 = calendar.getinstance();
    localcalendar1.set(calendar.hour_of_day, 0);
    localcalendar1.set(calendar.minute, 0);
    localcalendar1.set(calendar.second, 0);
    localcalendar1.set(calendar.millisecond, 0);
    date localdate1 = localcalendar1.gettime();

    long l1 = localdate1.gettime();




    calendar localcalendar2 = calendar.getinstance();
    localcalendar2.set(calendar.hour_of_day, 23);
    localcalendar2.set(calendar.minute, 59);
    localcalendar2.set(calendar.second, 59);
    localcalendar2.set(calendar.millisecond, 999);
    date localdate2 = localcalendar2.gettime();

    long l2 = localdate2.gettime();




    timeinfo localtimeinfo = new timeinfo();
    localtimeinfo.setstarttime(l1);
    localtimeinfo.setendtime(l2);
    return localtimeinfo;

  }




//  获取 昨天开始结束 时间


  public static timeinfo getyesterdaystartandendtime() {
    calendar localcalendar1 = calendar.getinstance();
    localcalendar1.add(calendar.day_of_month, -1);//5
    localcalendar1.set(calendar.hour_of_day, 0);//11
    localcalendar1.set(calendar.minute, 0);//12
    localcalendar1.set(calendar.second, 0);//13
    localcalendar1.set(calendar.millisecond, 0);//calendar.millisecond
    date localdate1 = localcalendar1.gettime();
    long l1 = localdate1.gettime();


    calendar localcalendar2 = calendar.getinstance();
    localcalendar2.add(calendar.day_of_month, -1);//5
    localcalendar2.set(calendar.hour_of_day, 23);//11
    localcalendar2.set(calendar.minute, 59);//12
    localcalendar2.set(calendar.second, 59);//13
    localcalendar2.set(calendar.millisecond, 999);//calendar.millisecond
    date localdate2 = localcalendar2.gettime();
    long l2 = localdate2.gettime();


    timeinfo localtimeinfo = new timeinfo();
    localtimeinfo.setstarttime(l1);
    localtimeinfo.setendtime(l2);
    return localtimeinfo;
  }





//  获取 前天开始结束 时间


  public static timeinfo getbeforeyesterdaystartandendtime() {
    calendar localcalendar1 = calendar.getinstance();
    localcalendar1.add(calendar.day_of_month, -2);
    localcalendar1.set(calendar.hour_of_day, 0);
    localcalendar1.set(calendar.minute, 0);
    localcalendar1.set(calendar.second, 0);
    localcalendar1.set(calendar.millisecond, 0);
    date localdate1 = localcalendar1.gettime();
    long l1 = localdate1.gettime();


    calendar localcalendar2 = calendar.getinstance();
    localcalendar2.add(calendar.day_of_month, -2);
    localcalendar2.set(calendar.hour_of_day, 23);
    localcalendar2.set(calendar.minute, 59);
    localcalendar2.set(calendar.second, 59);
    localcalendar2.set(calendar.millisecond, 999);
    date localdate2 = localcalendar2.gettime();
    long l2 = localdate2.gettime();
    timeinfo localtimeinfo = new timeinfo();
    localtimeinfo.setstarttime(l1);
    localtimeinfo.setendtime(l2);
    return localtimeinfo;
  }





  private static boolean issameday(long paramlong) {
    timeinfo localtimeinfo = gettodaystartandendtime();
    return (paramlong > localtimeinfo.getstarttime()) && (paramlong < localtimeinfo.getendtime());
  }


  private static boolean isyesterday(long paramlong) {
    timeinfo localtimeinfo = getyesterdaystartandendtime();
    return (paramlong > localtimeinfo.getstarttime()) && (paramlong < localtimeinfo.getendtime());
  }





  private static boolean isbeforeyesterday(long paramlong) {
    timeinfo localtimeinfo = getbeforeyesterdaystartandendtime();
    return (paramlong > localtimeinfo.getstarttime()) && (paramlong < localtimeinfo.getendtime());
  }








  public static boolean issameyear(int year) {
    calendar cal = calendar.getinstance();
    int curyear = cal.get(calendar.year);
//    log.e("","curyear="+curyear);//2015
    return curyear==year;
  }


。。。。。。

public class timeinfo {
  private long starttime;
  private long endtime;


  public long getstarttime() {
    return this.starttime;
  }


  public void setstarttime(long paramlong) {
    this.starttime = paramlong;
  }


  public long getendtime() {
    return this.endtime;
  }


  public void setendtime(long paramlong) {
    this.endtime = paramlong;
  }
}

以上这篇java判断今天,昨天,前天,不能用秒间隔的简单实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。