import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;
/**
* 时间工具类
*/
public class TimeUtils {
/**
* 指定日期加上天数后的日期
* @param num 为增加的天数
* @param newDate 创建时间
* @return
* @throws ParseException
*/
public static String plusDay(int num,String newDate) {
DateFormat format1 = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
Date d1 = null;
String DateToStr2 = "";
try {
d1 = format1.parse(newDate);
Calendar cl = Calendar.getInstance();
cl.setTime(d1);
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
String DateToStr = format.format(d1);
// System.out.println("当前时间 : " + DateToStr);
cl.setTime(d1);
cl.add(Calendar.DATE, num);
SimpleDateFormat format2 = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
DateToStr2 = format2.format(cl.getTime());
// System.out.println("加上一个月后的时间为: " + DateToStr2);
}catch(Exception e) {
DateToStr2 = "未知错误";
}
return DateToStr2;
}
//当前日期加上天数:
/**
* 获得当前时间
* @return 时间
*/
public static String getTimeMonth(){
long time=System.currentTimeMillis();//long now = android.os.SystemClock.uptimeMillis();
SimpleDateFormat format=new SimpleDateFormat("yyyy年MM月dd日");
Date d1=new Date(time);
String t1=format.format(d1);
return t1;
}
/**
* 获得当前时间
* @return 时间
*/
public static String getTimeSec(){
long time=System.currentTimeMillis();//long now = android.os.SystemClock.uptimeMillis();
SimpleDateFormat format=new SimpleDateFormat("HH:mm");
Date d1=new Date(time);
String t1=format.format(d1);
return t1;
}
/**
* 将一个时间格式转成另外一个时间格式
* @param cc_time
* @return
*/
public static String getTime(String cc_time)
{
String date1="";
try {
SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss", java.util.Locale.US);
Date date = sdf.parse(cc_time.toString());
SimpleDateFormat sdf2=new SimpleDateFormat("HH:mm");
date1=sdf2.format(date);
} catch (ParseException e) {
e.printStackTrace();
}
return date1;
}
//将时间转换为时间戳
public static String dateToStamp(String s)throws ParseException
{
String res;
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = simpleDateFormat.parse(s);
long ts = date.getTime();
res = String.valueOf(ts);
return res;
}
//将时间戳转换为时间
public static String stampToDate(String s)
{
String res;
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
long lt = new Long(s);
Date date = new Date(lt);
res = simpleDateFormat.format(date);
return res;
}
/**
* 将1,2,3,4,5,6,7转成一,二···
* @return
*/
public static String getWeekVaule(String week){
String week1 = "";
switch (week){
case "1":
week1 = "一";
break;
case "2":
week1 = "二";
break;
case "3":
week1 = "三";
break;
case "4":
week1 = "四";
break;
case "5":
week1 = "五";
break;
case "6":
week1 = "六";
break;
case "7":
week1 = "七";
break;
}
return week1;
}
/**
* 比较两个日期的大小,日期格式为yyyy-MM-dd
*
* @param str1 the first date
* @param str2 the second date
* @return true <br/>false
* true 为1大,false 为2大
*/
public static boolean isDateOneBigger(String str1, String str2) {
boolean isBigger = false;
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date dt1 = null;
Date dt2 = null;
try {
dt1 = sdf.parse(str1);
dt2 = sdf.parse(str2);
} catch (ParseException e) {
e.printStackTrace();
}
if (dt1.getTime() > dt2.getTime()) {
isBigger = true;
} else if (dt1.getTime() < dt2.getTime()) {
isBigger = false;
}
return isBigger;
}
/**
* 比较两个日期的大小,日期格式为yyyy-MM-dd
*
* @param str1 the first date
* @param str2 the second date
* @return true <br/>false
*/
public static boolean isDateOneBigger2(String str1, String str2) {
boolean isBigger = false;
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
Date dt1 = null;
Date dt2 = null;
try {
dt1 = sdf.parse(str1);
dt2 = sdf.parse(str2);
} catch (ParseException e) {
e.printStackTrace();
}
if (dt1.getTime() > dt2.getTime()) {
isBigger = true;
} else if (dt1.getTime() < dt2.getTime()) {
isBigger = false;
}
return isBigger;
}
/**
* 获得当前时间
* @return 时间
*/
public static String getTime(){
long time=System.currentTimeMillis();//long now = android.os.SystemClock.uptimeMillis();
SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm");
Date d1=new Date(time);
String t1=format.format(d1);
return t1;
}
/**
* 获得指定日期的前一天
* @param specifiedDay
* @return
* @throws Exception
*/
public static String getSpecifiedDayBefore(String specifiedDay){
//SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
Calendar c = Calendar.getInstance();
Date date=null;
try {
date = new SimpleDateFormat("yy-MM-dd").parse(specifiedDay);
} catch (ParseException e) {
e.printStackTrace();
}
c.setTime(date);
int day=c.get(Calendar.DATE);
c.set(Calendar.DATE,day-1);
String dayBefore=new SimpleDateFormat("yyyy-MM-dd").format(c.getTime());
return dayBefore;
}
/**
* 获得指定日期的后一天
* @param specifiedDay
* @return
*/
public static String getSpecifiedDayAfter(String specifiedDay){
Calendar c = Calendar.getInstance();
Date date=null;
try {
date = new SimpleDateFormat("yy-MM-dd").parse(specifiedDay);
} catch (ParseException e) {
e.printStackTrace();
}
c.setTime(date);
int day=c.get(Calendar.DATE);
c.set(Calendar.DATE,day+1);
String dayAfter=new SimpleDateFormat("yyyy-MM-dd").format(c.getTime());
return dayAfter;
}
/**
* 把时间根据时、分、秒转换为时间段
* @param StringDate
* @return
*/
public static String getTimes(String StringDate) {
Timestamp date = new Timestamp(stringToDate(StringDate).getTime());
Date now;
now = new Date();
long times = now.getTime() - date.getTime();
long month = times / ((24 * 60 * 60 * 1000) * 20);
long day = times / (24 * 60 * 60 * 1000);
long hour = (times / (60 * 60 * 1000) - day * 24);
long min = ((times / (60 * 1000)) - day * 24 * 60 - hour * 60);
long sec = (times / 1000 - day * 24 * 60 * 60 - hour * 60 * 60 - min * 60);
StringBuffer sb = new StringBuffer();
// sb.append("发表于:");
if (month > 0) {
SimpleDateFormat sdf = new SimpleDateFormat("MM-dd");
sb.append(sdf.format(date));
} else if (day > 0) {
if (day==1){
sb.append("昨天");
}else if(day == 2 ){
sb.append("前天");
}else{
sb.append(day + "天前");
}
} else if (hour > 0) {
sb.append(hour + "小时前");
} else if (min > 0) {
sb.append(min + "分钟前");
} else {
sb.append("刚刚");
}
String resultTimes = sb.toString();
return resultTimes;
}
/**
* 将string转成date
* @param str
* @return
*/
public static Date stringToDate(String str) {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = null;
try {
// Fri Feb 24 00:00:00 CST 2012
date = format.parse(str);
} catch (ParseException e) {
e.printStackTrace();
}
// // 2012-02-24
// date = java.sql.Date.valueOf(str);
return date;
}
}