资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

利用java如何实现判断日期-创新互联

创新互联www.cdcxhl.cn八线动态BGP香港云服务器提供商,新人活动买多久送多久,划算不套路!

创新互联是网站建设专家,致力于互联网品牌建设与网络营销,专业领域包括网站建设、网站制作、电商网站制作开发、微信小程序开发、微信营销、系统平台开发,与其他网站设计及系统开发公司不同,我们的整合解决方案结合了恒基网络品牌建设经验和互联网整合营销的理念,并将策略和执行紧密结合,且不断评估并优化我们的方案,为客户提供全方位的互联网品牌整合方案!

本篇文章给大家分享的是有关利用java如何实现判断日期,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。

实例如下:

一 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如何实现判断日期-创新互联
网页路径:http://cdkjz.cn/article/djihep.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220