您的位置:首页 > 编程语言 > Java开发

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()  //四舍五入

------------------------------------------------------------------
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  java