zoukankan      html  css  js  c++  java
  • Java获取今天/昨天/前天的某个时间点的方法

    开发时需要把如“今天 12:15” "昨天 21:21" "前天 03: 02"之类的String转换成Date,所以写了个方法,有更好的写法的话欢迎留言

     1 //获取今天、昨天、前天的某个时间的方法, 如"昨天 21:21" "前天 15:15"
     2     private static Date getFormatDate(String updated) throws Exception{
     3         if (updated.substring(0, 2).equals("今天")){
     4             String partOfDate = updated.substring(3);
     5             Calendar calendar = Calendar.getInstance();
     6             calendar.setTime(new Date());
     7             calendar.set(Calendar.HOUR_OF_DAY, 0);
     8             calendar.set(Calendar.MINUTE, 0);
     9             calendar.set(Calendar.SECOND, 0);
    10             SimpleDateFormat todaySdf = new SimpleDateFormat("yyyy-MM-dd");
    11             String today = todaySdf.format(calendar.getTime());
    12             String finalString = today + " " + partOfDate;
    13             SimpleDateFormat finalSdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
    14             return finalSdf.parse(finalString);
    15         }else if (updated.substring(0,2).equals("昨天")){
    16             String partOfDate = updated.substring(3);
    17             Calendar calendar = Calendar.getInstance();
    18             calendar.setTime(new Date());
    19             calendar.add(Calendar.DATE, -1);
    20             calendar.set(Calendar.HOUR_OF_DAY, 0);
    21             calendar.set(Calendar.MINUTE, 0);
    22             calendar.set(Calendar.SECOND, 0);
    23             SimpleDateFormat yesterdaySdf = new SimpleDateFormat("yyyy-MM-dd");
    24             String yesterday = yesterdaySdf.format(calendar.getTime());
    25             String finalString = yesterday + " " + partOfDate;
    26             SimpleDateFormat finalSdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
    27             return finalSdf.parse(finalString);
    28         }else if (updated.substring(0, 2).equals("前天")){
    29             String partOfDate = updated.substring(3);
    30             Calendar calendar = Calendar.getInstance();
    31             calendar.setTime(new Date());
    32             calendar.add(Calendar.DATE, -2);
    33             calendar.set(Calendar.HOUR_OF_DAY, 0);
    34             calendar.set(Calendar.MINUTE, 0);
    35             calendar.set(Calendar.SECOND, 0);
    36             SimpleDateFormat daysBeforeSdf = new SimpleDateFormat("yyyy-MM-dd");
    37             String daysBefore = daysBeforeSdf.format(calendar.getTime());
    38             String finalString = daysBefore + " " + partOfDate;
    39             SimpleDateFormat finalSdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
    40             return finalSdf.parse(finalString);
    41         }else {
    42             SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
    43             return sdf.parse(updated);
    44         }
    45     }

    Written on Dec. 14th, 2019

  • 相关阅读:
    Caused by: java.lang.UnsatisfiedLinkError: Couldn't load 。。。。
    final,finally,finalize
    Java位运算总结:位运算用途广泛《转》
    Ext.Ajax.request同步请求
    xstream 别名的用法<转>
    MySQL 获得当前日期时间(以及时间的转换)
    关于用xercesImpl包解析xml文件遇中文报错问题
    kafka java示例
    varnish、squid、apache、nginx缓存的对比<转>
    websphere性能设置和日常维护
  • 原文地址:https://www.cnblogs.com/LittleMike/p/12038462.html
Copyright © 2011-2022 走看看