几个常见的关于日期的问题_MySQL
时间:2021-07-01 10:21:17
帮助过:40人阅读
1. 怎样计算两个时间之间的间隔?
间隔=Date1.getTime()-Date2.getTime();得出来的是毫秒数.
除1000是秒,再除60是分,再除60是小时..............................
记住java标准库中所有时间类都以此为基础转化的,只是他写好了一些
转化的方法给你用而已.但都离不开这个毫秒数为基础.
2. t=Calendar.getInstance();m=t.get(t.MONTH)+1;这里为什么要加一?
在java语言里,date的month的取值范围是:0~11,与人们的自然表达上相差1。
3. 系统时间与当前日期的区别?
系统时间确切的说应该是
System.currentTimeMillis();
new Date()是当前日期,虽然它getTime();和System.currentTimeMillis();
一样,但System.currentTimeMillis();
4. 如何计算两个日期的天数差值?
long beginTime = beginDate.getTime();
long endTime2 = endDate.getTime();
long betweenDays = (long)((endTime - beginTime) / (1000 * 60 * 60 *24) + 0.5);
5. 如何比较日期时间大小?
第一种方法:
use Calendar object to compare
java.util.Calendar class can be used to compare date. In order to do this,
you guy should parse that string into int year, month, day and construct a
Calendar object, and then do comparison.
Below is a sample
StringTokenizer token = new StringTokenizer(your string,"-");
int year = Integer.parseInt(token.nextToken());
int month = Integer.parseInt(token.nextToken());
int day = Integer.parseInt(token.nextToken());
Calendar date = Calendar.getInstance();
date.set(year,month,day);
Calendar today = Calendar.getInstacne();
if(date.after(today)){
String newdir=year+month+day;
String wenjian = hh+mm+ss;
7. 怎么得到一个月的天数?
java.util.Calendar date = java.util.Calendar.getInstance();
System.out.println(date.getActualMaximum(date.DAY_OF_MONTH));