1. 程式人生 > >Android 判斷獲取的時間是否是今天、昨天

Android 判斷獲取的時間是否是今天、昨天

public class MainActivity extends Activity {
    private String str;
@Override
protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getTime();
getJudgetoDay();//判斷是否是今天
getJudgeYesterday();//判斷是否是昨天
}

    public void getJudgeYesterday
() { try { boolean flag = IsYesterday(str); if (flag == true) {//是昨天 //TODO } else {//不是昨天 //TODO } } catch (ParseException e) { e.printStackTrace(); } } public void getJudgetoDay() { try { boolean
flag = IsToday(str); if (flag == true) {//是今天 //TODO } else {//不是今天 //TODO } } catch (ParseException e) { e.printStackTrace(); } } private void getTime() { SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"
);//得到當前的時間 Date curDate = new Date(System.currentTimeMillis()); str = formatter.format(curDate); } /** * 判斷是否為今天(效率比較高) * * @param day 傳入的 時間 "2016-06-28 10:10:30" "2016-06-28" 都可以 * @return true今天 false不是 * @throws ParseException */ public static boolean IsToday(String day) throws ParseException { Calendar pre = Calendar.getInstance(); Date predate = new Date(System.currentTimeMillis()); pre.setTime(predate); Calendar cal = Calendar.getInstance(); Date date = getDateFormat().parse(day); cal.setTime(date); if (cal.get(Calendar.YEAR) == (pre.get(Calendar.YEAR))) { int diffDay = cal.get(Calendar.DAY_OF_YEAR) - pre.get(Calendar.DAY_OF_YEAR); if (diffDay == 0) { return true; } } return false; } /** * 判斷是否為昨天(效率比較高) * * @param day 傳入的 時間 "2016-06-28 10:10:30" "2016-06-28" 都可以 * @return true今天 false不是 * @throws ParseException */ public static boolean IsYesterday(String day) throws ParseException { Calendar pre = Calendar.getInstance(); Date predate = new Date(System.currentTimeMillis()); pre.setTime(predate); Calendar cal = Calendar.getInstance(); Date date = getDateFormat().parse(day); cal.setTime(date); if (cal.get(Calendar.YEAR) == (pre.get(Calendar.YEAR))) { int diffDay = cal.get(Calendar.DAY_OF_YEAR) - pre.get(Calendar.DAY_OF_YEAR); if (diffDay == -1) { return true; } } return false; } public static SimpleDateFormat getDateFormat() { if (null == DateLocal.get()) { DateLocal.set(new SimpleDateFormat("yyyy-MM-dd", Locale.CHINA)); } return DateLocal.get(); } private static ThreadLocal<SimpleDateFormat> DateLocal = new ThreadLocal<SimpleDateFormat>(); }

相關推薦

sqlserver 在做Sql Serve獲取表中今天昨天本週上週本月上月等資料,這時候就需要使用DATEDIFF()函式及GetDate()函數了。

DATEDIFF ( datepart , startdate , enddate ) 釋義:計算時間差 datepare值:year | quarter | month | week | day | hour | minute | second | millisecond startdate:

Android 判斷獲取時間是否是今天昨天

public class MainActivity extends Activity { private String str; @Override protected void onCreate(Bundle savedInstanceState) {

關於python時間timedatetimedate之間轉換,獲取今天昨天上週上月去年的時間

一、time、datetime、date之間轉換 概念: 首先需要理解三個名詞: 時間戳(int), 時間陣列, 日期格式(str) utc time : 國際時間(倫敦時間) localtime: 本地時間時間(如北京時間 = 倫敦時間 + 8小時) 國際時間獲取: time.gm

判斷日期時間是否為今天昨天

function isToday(str){     var d = new Date(str.replace(/-/g,"/"));     var todaysDate = new Date();     if(d.setHours(0,0,0,0) == todaysD

PHP計算今天昨天本周本月上月開始時間和結束時間

mkt last strong spa mon php 結束時間 nbsp 時間 PHP計算今天、昨天、本周、本月、上月開始時間和結束時間 $today = date(‘Y-m-d H:i:s‘,mktime(0,0,0,date(‘m‘),date(‘d‘),date(

【Sql Server函數】日期時間函數日期查詢今天昨天7天內30天的數據

本周 varchar last 類型 nth weight 所有 convert ted 今天的所有數據:select * from 表名 where DateDiff(dd,datetime類型字段,getdate())=0 昨天的所有數據:select * from

Sql(時間):日期查詢-SQL查詢今天昨天7天內30天

今天的所有資料:select * from 表名 where DateDiff(dd,datetime型別欄位,getdate())=0 昨天的所有資料:select * from 表名 where DateDiff(dd,datetime型別欄位,getdate())=1

時間轉化成今天昨天和前天格式

#pragma mark - 將時間轉化為今天、昨天、前天 - (NSString *)formatting { NSDateFormatter *outputFormatter= [[NSDateFormatter alloc] init]; [outpu

時間戳轉化為今天昨天明天(字串格式)

    時間戳,相信大家一定都不陌生,伺服器經常會傳回來時間戳,需要我們對時間戳進行處理。各種麻煩不斷,比如轉為為明天,今天,昨天,或者跟現在時間相對比,過了多長時間,轉化為標準格式啊...各種情況,寫了一個工具類,來分享下... package com.woyou.ut

mysql查詢今天昨天近7天近30天本月上一月的SQL語句

時間 中比 code mat 信息 where 從大到小 案例 rdate mysql查詢今天、昨天、近7天、近30天、本月、上一月的SQL語句 這篇文章主要介紹了mysql查詢今天、昨天、近7天、近30天、本月、上一月的SQL語句,一般在一些統計報表中比較常用這個時間段,

MYSQL查詢今天昨天7天前30天本月數據

form mysq sql _for interval 時間 for days to_days 今天: SELECT * FROM 表名 WHERE TO_DAYS( 時間字段名) = TO_DAYS(NOW()); 昨天: SELECT * FROM 表名 WHERE T

SQL語句來查詢今天昨天7天內30天的數據,經典!

diff 指定 first 內容 輸出 har edi time nth ---恢復內容開始--- 今天的所有數據:select * from 表名 where DateDiff(dd,datetime類型字段,getdate())=0 昨天的所有數據:select *

SQL查詢今天昨天7天內30天 - 轉

mon getdate ont 註意 修改 get 當前時間 指定 body 今天的所有數據:select * from 表名 where DateDiff(dd,datetime類型字段,getdate())=0 昨天的所有數據:select * from 表名 w

sql 語句查詢今天昨天近7天近30天一個月內上一月 資料

SQL語句如下: 查詢當天的所有資料 SELECT * FROM 表名 WHERE DATEDIFF(欄位,NOW()) = 0 SELECT * FROM 表名 WHERE to_days(時間欄位名) = to_days(now()); 查詢昨天的所有

JAVA獲取日期:今天昨天,本週,本月,本年

/** * 日期工具類 */ public class DateUtils { /** * 獲取今天 * @return String * */ public static String getToday(){ return new SimpleDateFormat("

Sql Server日期查詢-SQL查詢今天昨天7天內30天

注: SQL查詢某一段的資料用的比較多,本人記性差,就只有轉載一下辣! 今天的所有資料:select * from 表名 where DateDiff(dd,datetime型別欄位,getdate())=0 昨天的所有資料:select * from 表名 where DateDiff(dd,d

Mysql SQL查詢今天昨天n天內第n天的資料 Mysql SQL查詢今天昨天n天內第n天

Mysql SQL查詢今天、昨天、n天內、第n天 注意:因為對索引列使用函式會導致索引失效,如果查詢時需要使用到索引,請使用MySQL DATE_SUB() 函式 查詢當天的所有資料 SELECT * FROM 表名 WHERE DATEDIFF(欄位,NOW())

SQL語句查詢今天昨天近7天近30天一個月內上一月 資料

SQL語句如下: 查詢當天的所有資料   SELECT * FROM 表名 WHERE DATEDIFF(欄位,NOW()) = 0 SELECT * FROM 表名 WHERE to_days(時間欄位名) = to_days(now());

SQL日期查詢-SQL查詢今天昨天7天內30天,年

近期程式開發中,需要對時間進行比較,此時需要用到SQL的時間查詢系列功能,綜合整理資料如下: 今天的所有資料:select * from 表名 where DateDiff(dd,datetime型別欄位,getdate())=0 昨天的所有資料:select * from&nbs

sqlserver查詢今天昨天本週上週本月上月等資料

在做Sql Server開發的時候有時需要獲取表中今天、昨天、本週、上週、本月、上月等資料,這時候就需要使用DATEDIFF()函式及GetDate()函數了。 DATEDIFF ( datepart , startdate , enddate ) 釋義:計算時間差 datepare值:ye