java和js获取当前天之后或之前7天(任意)日期
2024-08-24 07:57:32
一.获取过去第几天的日期(- 操作) 或者 未来 第几天的日期( + 操作)
/**
* 获取过去第几天的日期(- 操作) 或者 未来 第几天的日期( + 操作)
*
* @param past
* @return
*/
public static String getPastDate(int past) {
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) - past);
Date today = calendar.getTime();
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
String result = format.format(today);
Log.e(null, result);
return result;
}
二.获得N小时前(- 操作)的时间或者N小时后(+ 操作)的时间
private static void getHourPastOrFutureTime() {
try {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Calendar calendar=Calendar.getInstance();
/* HOUR_OF_DAY 指示一天中的小时 */
calendar.set(Calendar.HOUR_OF_DAY, calendar.get(Calendar.HOUR_OF_DAY) + 1);
System.out.println("一个小时前的时间:" + sdf.format(calendar.getTime()));
System.out.println("当前时间:" + sdf.format(new Date())); /*当前时间 String 类型--->Date类型*/
Date currentDateTime = sdf.parse(sdf.format(new Date()));
} catch (ParseException e1) {
e1.printStackTrace();
}
}
三.Js种获取相应对日期的处理方式:
方法一:
function getNowFormatDate() {
var date = new Date();
var seperator1 = "-";
var seperator2 = ":";
var month = date.getMonth() + 1;
var strDate = date.getDate();
if (month >= 1 && month <= 9) {
month = "0" + month;
}
if (strDate >= 0 && strDate <= 9) {
strDate = "0" + strDate;
}
var currentdate = date.getFullYear() + seperator1 + month + seperator1 + strDate
+ " " + date.getHours() + seperator2 + date.getMinutes()
+ seperator2 + date.getSeconds();
return currentdate;
}
方法二:
// 对Date的扩展,将 Date 转化为指定格式的String
// 月(M)、日(d)、小时(h)、分(m)、秒(s)、季度(q) 可以用 1-2 个占位符,
// 年(y)可以用 1-4 个占位符,毫秒(S)只能用 1 个占位符(是 1-3 位的数字)
// 例子:
// (new Date()).Format("yyyy-MM-dd hh:mm:ss.S") ==> 2006-07-02 08:09:04.423
// (new Date()).Format("yyyy-M-d h:m:s.S") ==> 2006-7-2 8:9:4.18
Date.prototype.Format = function(fmt)
{ //author: meizz
var o = {
"M+" : this.getMonth()+1, //月份
"d+" : this.getDate(), //日
"h+" : this.getHours(), //小时
"m+" : this.getMinutes(), //分
"s+" : this.getSeconds(), //秒
"q+" : Math.floor((this.getMonth()+3)/3), //季度
"S" : this.getMilliseconds() //毫秒
};
if(/(y+)/.test(fmt))
fmt=fmt.replace(RegExp.$1, (this.getFullYear()+"").substr(4 - RegExp.$1.length));
for(var k in o)
if(new RegExp("("+ k +")").test(fmt))
fmt = fmt.replace(RegExp.$1, (RegExp.$1.length==1) ? (o[k]) : (("00"+ o[k]).substr((""+ o[k]).length)));
return fmt;
}
调用方法:
var time1 = new Date().format("yyyy-MM-dd HH:mm:ss");
var time2 = new Date().format("yyyy-MM-dd");
js获取当前日期前后N天的方法:
function GetDateStr(AddDayCount) {
var dd = new Date();
dd.setDate(dd.getDate()+AddDayCount);//获取AddDayCount天后的日期
var y = dd.getFullYear();
var m = dd.getMonth()+1;//获取当前月份的日期
var d = dd.getDate();
return y+"-"+m+"-"+d;
}
我总结的其他关于日期和时间的博文:
http://www.cnblogs.com/DreamDrive/p/5735758.html
http://www.cnblogs.com/DreamDrive/p/4093622.html
最新文章
- 课堂 一些代码的叫法 JavaScript js
- php json与xml序列化/反序列化
- Python3学习(3)-高级篇
- Perfection Kills
- 安卓中bundle的使用
- NOIP2003 传染病控制
- Spring MVC使用commons fileupload实现文件上传功能
- lnmp安装fileinfo扩展
- 如何实现 Excel方式二维变色提示的 m*n 表格
- 一起看看2016中国第三届CSS开发者大会有哪些大咖演讲
- 「SDOI2017」树点涂色 解题报告
- idea 引入web、配制tomcat
- 刷题的model格式(请用英文)
- Android canvast View 代码实例
- android 当ListView滚动时自动调用 onCheckedChanged 导致CheckBox 状态不停变化 的解决办法
- Python练习笔记——采用生成器函数实现两数之间的素数计算
- mysql工具——mysqlcheck(MYISAM)
- Maven学习(六)-----Maven仓库的详细介绍
- POJ3480 John 博弈论 anti-nim anti-SG
- Android系统移植与调试之------->如何添加一个adb wifi无线调试的功能【开发者选项】-【Wifi调试】