자바 판단 오늘, 어제, 그저께, 초 간격으로 할 수 없는 간단한 실례

인스턴스는 다음과 같습니다.

  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;
  }
}
이상의 자바 판단은 오늘, 어제, 그저께, 초 간격으로 할 수 없는 간단한 실례가 바로 편집자가 여러분에게 공유한 모든 내용입니다. 여러분께 참고가 되고 저희를 많이 사랑해 주시기 바랍니다.

좋은 웹페이지 즐겨찾기