package com.java456.util;
import java.net.URLEncoder;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import javax.xml.crypto.Data;
import net.sf.json.JSON;
import net.sf.json.JSONObject;
public class DateUtil {
public static void main(String s[]) throws Exception{
System.out.println(getDatePoor(new Date(),formatString("2019-11-14 06:30", "yyyy-MM-dd HH:mm")));
//2017-01-31
}
/**
* yyyyMMdd hhmmssSSS
* 日期对象转字符串
*/
public static String formatDate(Date date,String format){
String result="";
SimpleDateFormat sdf=new SimpleDateFormat(format);
if(date!=null){
result=sdf.format(date);
}
return result;
}
/**
* 字符串转日期对象
* @param str
* @param format
* @return
* @throws Exception
*/
public static Date formatString(String str,String format){
if(StringUtil.isEmpty(str)){
return null;
}
SimpleDateFormat sdf=new SimpleDateFormat(format);
try {
return sdf.parse(str);
} catch (ParseException e) {
e.printStackTrace();
}
return null;
}
public static String getCurrentDateStr()throws Exception{
Date date=new Date();
SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMddHHmmss");
return sdf.format(date);
}
/**
* 输入一个月份,2017-01
* 输出 2017-01-31
*/
public static String getDays(String str){
str = dateAddMonth(str, "yyyy-MM", 1);//2017-01---2017-02
str = str+"-01";//2017-02-01
str = dateAddDay(str, "yyyy-MM-dd", -1);////2017-01-31
return str;
}
/**
* 算2个时间差几分
* @param end
* @param begin
* @return
*/
public static long date_between(Date end,Date begin ){
/*SimpleDateFormat dfs = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date begin=dfs.parse("2004-01-02 11:30:24");
Date end = dfs.parse("2004-01-02 11:31:40");*/
long between=(end.getTime()-begin.getTime())/1000;//除以1000是为了转换成秒
long min=between/60;
System.out.println("差几分"+min);
return min;
}
/**
* 任意格式的日期
* @param date1
* @param date2
* @param format 日期格式 yyyy-MM-dd HH:mm:ss SSS
* @return 1比2大 返回1 相等返回0 1比2小 返回-1
*/
public static int compareTo(String date1,String date2,String format){
return DateUtil.formatString(date1, format).compareTo(DateUtil.formatString(date2, format));
}
/**
* 任意格式的日期
* @return 1比2大 返回1 相等返回0 1比2小 返回-1
*/
public static int compareTo(Date date1,Date date2 ){
return date1.compareTo(date2);
}
/**
* 任意格式 加1天 或者 加-1天
* @param dateStr 时间字符串 2018-05-05 12:11
* @param dateFormat 时间格式 yyyy-MM-dd HH:mmssSSS
* @param n 加多少天
*/
public static String dateAddDay(String dateStr,String dateFormat, int n) {
try {
SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
Calendar cd = Calendar.getInstance();
cd.setTime(sdf.parse(dateStr));
cd.add(Calendar.DATE, n);// 增加一天
// cd.add(Calendar.MONTH, n);//增加一个月
return sdf.format(cd.getTime());
} catch (Exception e) {
return null;
}
}
/**
* 任意格式 加多少月 或者 减多少月
* @param dateStr 时间字符串 2018-05-05 12:11
* @param dateFormat 时间格式 yyyy-MM-dd HH:mm 时间格式与真实时间吻合
* @param n 加10月 10 减10月 -10
*/
public static String dateAddMonth(String dateStr,String dateFormat, int n) {
try {
SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
Calendar cd = Calendar.getInstance();
cd.setTime(sdf.parse(dateStr));
//cd.add(Calendar.DATE, n);// 增加一天
cd.add(Calendar.MONTH, n);//增加一个月
return sdf.format(cd.getTime());
} catch (Exception e) {
return null;
}
}
/**
* 任意格式 加多少小时 或者 减多少小时
* @param dateStr 时间字符串 2018-05-05 12:11
* @param dateFormat 时间格式 yyyy-MM-dd HH:mmssSSS
* @param n 加10小时 +10 减10小时 -10
*/
public static String dateAddHour(String dateStr,String dateFormat, int n) {
try {
SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
Calendar cd = Calendar.getInstance();
cd.setTime(sdf.parse(dateStr));
cd.add(Calendar.HOUR, n);
return sdf.format(cd.getTime());
} catch (Exception e) {
return null;
}
}
/**
* 给出时间 返回星期几
* @param datetime 2018-01-01
* @return 星期一
*/
public static String dateToWeek(String datetime) {
SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd");
String[] weekDays = { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" };
Calendar cal = Calendar.getInstance(); // 获得一个日历
Date datet = null;
try {
datet = f.parse(datetime);
cal.setTime(datet);
} catch (Exception e) {
e.printStackTrace();
}
int w = cal.get(Calendar.DAY_OF_WEEK) - 1; // 指示一个星期中的某天。
if (w < 0)
w = 0;
return weekDays[w];
}
/**
* #计算date1 比date2 多几天 几小时
* @param date1 是当前时间 比较大的时间
* @param date2 是之前的时间
* @return
*/
public static JSONObject getDatePoor(Date date1 , Date date2 ) {
long nd = 1000 * 24 * 60 * 60;//每天毫秒数
long nh = 1000 * 60 * 60;//每小时毫秒数
long nm = 1000 * 60;//每分钟毫秒数
long diff = date1.getTime() - date2.getTime(); // 获得两个时间的毫秒时间差异
long day = diff / nd; // 计算差多少天
long hour = diff % nd / nh; // 计算差多少小时
long min = diff % nd % nh / nm; // 计算差多少分钟
JSONObject result =new JSONObject();
result.put("day", day);
result.put("hour", hour);
result.put("min", min);
System.out.println( day + "天" + hour + "小时" + min + "分钟");
return result;
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源详情
资源评论
资源推荐
收起资源包目录
山东高考志愿填报辅助系统.zip
(271个子文件)
mvnw.cmd 6KB
layui.css 71KB
layer.css 14KB
layui.mobile.css 10KB
layim.css 9KB
laydate.css 7KB
style.css 5KB
base.css 5KB
base.css 4KB
code.css 1KB
manage_base.css 760B
manage_base.css 159B
iconfont.eot 41KB
59.gif 10KB
22.gif 10KB
24.gif 8KB
13.gif 7KB
16.gif 7KB
39.gif 6KB
64.gif 6KB
63.gif 6KB
50.gif 6KB
loading-0.gif 6KB
4.gif 6KB
1.gif 5KB
42.gif 5KB
71.gif 5KB
21.gif 5KB
20.gif 5KB
29.gif 5KB
70.gif 4KB
5.gif 4KB
17.gif 4KB
27.gif 4KB
9.gif 4KB
44.gif 4KB
11.gif 4KB
8.gif 4KB
3.gif 4KB
23.gif 4KB
34.gif 4KB
41.gif 4KB
38.gif 4KB
65.gif 3KB
32.gif 3KB
45.gif 3KB
7.gif 3KB
12.gif 3KB
26.gif 3KB
60.gif 3KB
2.gif 3KB
40.gif 3KB
25.gif 3KB
19.gif 3KB
66.gif 3KB
18.gif 3KB
46.gif 3KB
10.gif 3KB
28.gif 3KB
51.gif 3KB
57.gif 3KB
67.gif 3KB
0.gif 3KB
48.gif 3KB
43.gif 3KB
30.gif 2KB
61.gif 2KB
33.gif 2KB
69.gif 2KB
14.gif 2KB
47.gif 2KB
36.gif 2KB
49.gif 2KB
58.gif 2KB
6.gif 2KB
54.gif 2KB
53.gif 2KB
56.gif 2KB
62.gif 2KB
31.gif 2KB
55.gif 2KB
35.gif 2KB
15.gif 2KB
loading-2.gif 2KB
37.gif 1KB
68.gif 1KB
52.gif 777B
loading-1.gif 701B
.gitignore 395B
blog_manage.html 10KB
add_update.html 10KB
lunbo_manage.html 7KB
add_update.html 6KB
menu_manage.html 6KB
stu_manage.html 6KB
view.html 6KB
teach_select_manage.html 6KB
blog_type_manage.html 6KB
link_manage.html 6KB
teach_manage.html 5KB
共 271 条
- 1
- 2
- 3
CSDN_SYSU
- 粉丝: 1
- 资源: 3
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论1