nynw.net
当前位置:首页 >> jAvA时间加减的问题 >>

jAvA时间加减的问题

df.format(new Date(d.getTime() - 30 * 24 * 60 * 60 * 1000L)) 最后加个L,这个计算超过了Integer最大值,变成负数了,转换为Long就好了

1.用java.util.Calender来实现 Calendar calendar=Calendar.getInstance();calendar.setTime(new Date());System.out.println(calendar.get(Calendar.DAY_OF_MONTH));//今天的日期calendar.set(Calendar.DAY_OF_MONTH,calendar.get(Calendar.DAY...

String str = "2013-07-18";SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");try {Date myDate = formatter.parse(str);Calendar c = Calendar.getInstance();c.setTime(myDate);c.add(Calendar.MONTH, 8);myDate = c.getTime...

附上代码: Date date = new Date("2014/1/10 18:20"); Date date2 = new Date("2014/1/11 3:5"); long temp = date2.getTime() - date.getTime(); //相差毫秒数 long hours = temp / 1000 / 3600; //相差小时数 long temp2 = temp % (1000 * 3...

给你一个参考如下: import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; public class DateTestUtil { public static void main(String[] args) throws Exception { SimpleDateFormat sdf=new SimpleDateFo...

Java代码 public class test1 { public static void main(String[] args) { Calendar c = Calendar.getInstance(); int year=c.get(Calendar.YEAR); int month=c.get(Calendar.MONTH)+1; int date=c.get(Calendar.DATE); System.out.println("今...

import java.util.Date; import org.apache.commons.lang3.time.DateUtils; public class Test { public static void main(String[] args) { Date now = new Date(); // 减一小时 Date add = DateUtils.addHours(now, -1); // 加一小时 Date sub...

Java代码 public class test1 { public static void main(String[] args) { Calendar c = Calendar.getInstance(); int year=c.get(Calendar.YEAR); int month=c.get(Calendar.MONTH)+1; int date=c.get(Calendar.DATE); System.out.println("今...

最简单的方法应该是,换成毫秒,直接加减之后再换回24进制时

Calendar currentDate = new GregorianCalendar(); currentDate.setFirstDayOfWeek(Calendar.MONDAY); currentDate.set(Calendar.HOUR_OF_DAY, 0); //小时加减 正数是加,负数是减 currentDate.set(Calendar.MINUTE, 0); //分钟加减 currentDate...

网站首页 | 网站地图
All rights reserved Powered by www.nynw.net
copyright ©right 2010-2021。
内容来自网络,如有侵犯请联系客服。zhit325@qq.com