您的位置:首页 > 移动开发 > Android开发

Android开发处理时间的小方法

2017-05-04 13:04 274 查看
Android开发处理时间的小方法

1、字符串转时间戳

public static String getString_time(String user_time) {
String re_time = null;
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
Date d;
try {
d = sdf.parse(user_time);
long l = d.getTime();
String str = String.valueOf(l);
re_time = str.substring(0, 10);
}catch (ParseException e) {
// TODO Auto-generated catch block e.printStackTrace();
}
return re_time;
}


2、获取当前的时间

public static String gettime(){
SimpleDateFormat  sDateFormat  =  new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
String date = sDateFormat.format(new java.util.Date());
return date;
}


3、今天是星期几?

public static String getway(){
final Calendar c = Calendar.getInstance();
c.setTimeZone(TimeZone.getTimeZone("GMT+8:00"));
String mWay = String.valueOf(c.get(Calendar.DAY_OF_WEEK));
if("1".equals(mWay)){
mWay ="天";
}else if("2".equals(mWay)){
mWay ="一";
}else if("3".equals(mWay)){
mWay ="二";
}else if("4".equals(mWay)){
mWay ="三";
}else if("5".equals(mWay)){
mWay ="四";
}else if("6".equals(mWay)){
mWay ="五";
}else if("7".equals(mWay)){
mWay ="六";
}
return mWay;
}


4、传进一个时间段,判断当前时间是否在这个区间时间内

public static boolean isInTime(String sourceTime, String curTime) {
if (sourceTime == null || !sourceTime.contains("-") ||  !sourceTime.contains(":")) {
throw new IllegalArgumentException("Illegal Argument arg:" + sourceTime);
}
if (curTime == null || !curTime.contains(":")) {
throw new IllegalArgumentException("Illegal Argument arg:" + curTime);
}
String[] args = sourceTime.split("-");
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
try {
long now = sdf.parse(curTime).getTime();
long start = sdf.parse(args[0]).getTime();
long end = sdf.parse(args[1]).getTime();
if (args[1].equals("00:00")) {
args[1] = "24:00";
}
if (end < start) {
if (now >= end && now < start) {
return false;
} else {
return true;
}
}
else {
if (now >= start && now < end) {
return true;
} else {
return false;
}
}
} catch (ParseException e) {
e.printStackTrace();
throw new IllegalArgumentException("Illegal Argument arg:" + sourceTime);
}

}


5、传进一个时间,根据这个时间计算下一刻的时间

public static String computationTime(String time) {
if (time.equals(""))
return "";
int hour = Integer.valueOf(time.substring(0, 2));
int minute = Integer.valueOf(time.substring(3, 5));
if (minute + 1 == 60) {
minute = 0;
hour += 1;
if (hour == 24) {
hour = 0;
}
} else {
minute += 1;
}
time = hour < 10 ? "0" + hour + ":" : hour + ":";
time += minute < 10 ? "0" + minute : minute + "";
return time;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  android 时间处理
相关文章推荐