Java中的一些日期操作
2016-12-01 14:04
337 查看
获取当前日期的下一天:
//测试方法
public static void main(String args[]){
SimpleDateFormat sdf = new SimpleDateFormat("yy-MM-dd");
Date date = new Date();// 定义Date
Date date2 = getDate(date);
System.out.println(sdf.format(date2));
}
// 公共方法
public static Date getDate(Date date){
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.add(Calendar.DAY_OF_MONTH, 1);
Date date1 = new Date(calendar.getTimeInMillis());
return date1;
}
-----------------------------------------------------------
获取当前的月份:
Calendar cal = Calendar.getInstance();
cal.setTimeInMillis( System.currentTimeMillis());
int month = cal.get(Calendar.MONTH) + 1; // 因为月是从0开始算起的,所以加个1
-----------------------------------------------------------
在MYSQL中字符串连接使用的是concat内置函数。
可以写为:select * from myDB where name =concat('a','bc')
在SQL Server中字符串连接用+号。可以写为:select * from myDB where name ='a'+'bc'
在Oracle中字符串连接用的是||号
------------------------------------------------------------
Java计算两个日期时间相差几天,几小时,几分钟:
其实好简单就可以实现jsp,java中计算两个时间差了
public class Test {
public void dateDiff(String startTime, String endTime, String format) {
//按照传入的格式生成一个simpledateformate对象
SimpleDateFormat sd = new SimpleDateFormat(format);
long nd = 1000*24*60*60;//一天的毫秒数
long nh = 1000*60*60;//一小时的毫秒数
long nm = 1000*60;//一分钟的毫秒数
long ns = 1000;//一秒钟的毫秒数long diff;try {
//获得两个时间的毫秒时间差异
diff = sd.parse(endTime).getTime() - sd.parse(startTime).getTime();
long day = diff/nd;//计算差多少天
long hour = diff%nd/nh;//计算差多少小时
long min = diff%nd%nh/nm;//计算差多少分钟搜索
long sec = diff%nd%nh%nm/ns;//计算差多少秒//输出结果
System.out.println("时间相差:"+day+"天"+hour+"小时"+min+"分钟"+sec+"秒。");
-----------------------------------------------------------------
向上取整:Math.ceil() //只要有小数都+1
向下取整:Math.floor() //不取小数
四舍五入:Math.round() //四舍五入
------------------------------------------------------------------
//测试方法
public static void main(String args[]){
SimpleDateFormat sdf = new SimpleDateFormat("yy-MM-dd");
Date date = new Date();// 定义Date
Date date2 = getDate(date);
System.out.println(sdf.format(date2));
}
// 公共方法
public static Date getDate(Date date){
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.add(Calendar.DAY_OF_MONTH, 1);
Date date1 = new Date(calendar.getTimeInMillis());
return date1;
}
-----------------------------------------------------------
获取当前的月份:
Calendar cal = Calendar.getInstance();
cal.setTimeInMillis( System.currentTimeMillis());
int month = cal.get(Calendar.MONTH) + 1; // 因为月是从0开始算起的,所以加个1
-----------------------------------------------------------
在MYSQL中字符串连接使用的是concat内置函数。
可以写为:select * from myDB where name =concat('a','bc')
在SQL Server中字符串连接用+号。可以写为:select * from myDB where name ='a'+'bc'
在Oracle中字符串连接用的是||号
------------------------------------------------------------
Java计算两个日期时间相差几天,几小时,几分钟:
其实好简单就可以实现jsp,java中计算两个时间差了
public class Test {
public void dateDiff(String startTime, String endTime, String format) {
//按照传入的格式生成一个simpledateformate对象
SimpleDateFormat sd = new SimpleDateFormat(format);
long nd = 1000*24*60*60;//一天的毫秒数
long nh = 1000*60*60;//一小时的毫秒数
long nm = 1000*60;//一分钟的毫秒数
long ns = 1000;//一秒钟的毫秒数long diff;try {
//获得两个时间的毫秒时间差异
diff = sd.parse(endTime).getTime() - sd.parse(startTime).getTime();
long day = diff/nd;//计算差多少天
long hour = diff%nd/nh;//计算差多少小时
long min = diff%nd%nh/nm;//计算差多少分钟搜索
long sec = diff%nd%nh%nm/ns;//计算差多少秒//输出结果
System.out.println("时间相差:"+day+"天"+hour+"小时"+min+"分钟"+sec+"秒。");
-----------------------------------------------------------------
向上取整:Math.ceil() //只要有小数都+1
向下取整:Math.floor() //不取小数
四舍五入:Math.round() //四舍五入
------------------------------------------------------------------
相关文章推荐
- java对日期的一些操作
- java 关于使用java1.8的localDateTime日期操作的基本使用以及一些使用技巧 持续更新...
- 整理的一些常用的Java日期时间上面的操作
- java中的System类的一些应用和日期的操作实例
- Java日期时间操作的一些方法
- 关于java时间与日期的一些操作
- 处理java日期的一些操作
- java之日期的一些操作
- (转)java中对于文件属性的一些操作
- Java 中对日期的操作
- 用JAVA操作日期类型
- 收藏:java中关于时间日期操作的常用函数
- JAVA日期操作
- java中关于时间日期操作地常用函数
- Java日期操作常用方法
- java中对日期的加减操作
- JAVA中的日期时间操作
- java中关于时间日期操作的常用函数
- java中关于时间日期操作的常用函数
- Java中一些关于日期、日期格式、日期的解析和日期的计算