Calendar calendar=Calendar.getInstance(); calendar.set(2007,6,30); long timeone=calendar.getTimeMillis(); calendar.set(calendar.get(Calendar.YEAR),(calendar.get(Calendar.MONTH))+1,calendar.get(Calendar.DAY_OF_MONTH)); long timetwo=calendar.getTimeMillis(); long time=(timetwo-tiemone)/(1000*60*60*24); System.out.println("日差:"+time);
try { SimpleDateFormat sf = new SimpleDateFormat("yyyyMMdd"); Date date1 = sf.parse("20070730"); Calendar c1 = Calendar.getInstance(); c1.setTime(date1); Calendar c2 = Calendar.getInstance(); int count = 0; while (c1.before(c2)) { count++; c1.add(Calendar.Date, 1); } System.out.println("days is " + count); } catch (Throwable e) { e.printStackTrance(); }
try { SimpleDateFormat sf = new SimpleDateFormat("yyyyMMdd"); Date date1 = sf.parse("20070730"); Calendar c1 = Calendar.getInstance(); c1.setTime(date1); Calendar c2 = Calendar.getInstance(); int count = 0; while (c1.before(c2)) { count++; c1.add(Calendar.Date, 1); } System.out.println("days is " + count); } catch (Throwable e) { e.printStackTrance(); } Calendar calendar=Calendar.getInstance(); calendar.set(2007,6,30); long timeone=calendar.getTimeMillis(); calendar.set(calendar.get(Calendar.YEAR),(calendar.get(Calendar.MONTH))+1,calendar.get(Calendar.DAY_OF_MONTH)); long timetwo=calendar.getTimeMillis(); long time=(timetwo-tiemone)/(1000*60*60*24); System.out.println("日差:"+time); 两者的算法都类似。都可以
calendar.set(2007,6,30);
long timeone=calendar.getTimeMillis();
calendar.set(calendar.get(Calendar.YEAR),(calendar.get(Calendar.MONTH))+1,calendar.get(Calendar.DAY_OF_MONTH));
long timetwo=calendar.getTimeMillis();
long time=(timetwo-tiemone)/(1000*60*60*24);
System.out.println("日差:"+time);
SimpleDateFormat sf = new SimpleDateFormat("yyyyMMdd");
Date date1 = sf.parse("20070730");
Calendar c1 = Calendar.getInstance();
c1.setTime(date1);
Calendar c2 = Calendar.getInstance();
int count = 0;
while (c1.before(c2)) {
count++;
c1.add(Calendar.Date, 1);
}
System.out.println("days is " + count);
} catch (Throwable e) {
e.printStackTrance();
}
SimpleDateFormat sf = new SimpleDateFormat("yyyyMMdd");
Date date1 = sf.parse("20070730");
Calendar c1 = Calendar.getInstance();
c1.setTime(date1);
Calendar c2 = Calendar.getInstance();
int count = 0;
while (c1.before(c2)) {
count++;
c1.add(Calendar.Date, 1);
}
System.out.println("days is " + count);
} catch (Throwable e) {
e.printStackTrance();
}
Calendar calendar=Calendar.getInstance();
calendar.set(2007,6,30);
long timeone=calendar.getTimeMillis();
calendar.set(calendar.get(Calendar.YEAR),(calendar.get(Calendar.MONTH))+1,calendar.get(Calendar.DAY_OF_MONTH));
long timetwo=calendar.getTimeMillis();
long time=(timetwo-tiemone)/(1000*60*60*24);
System.out.println("日差:"+time);
两者的算法都类似。都可以
public static Timestamp valueOf(String s)将使用 JDBC 时间戳转义格式的 String 对象转换为 Timestamp 值。 参数:
s - 使用 yyyy-mm-dd hh:mm:ss.fffffffff 格式的时间戳
返回:
相应的 Timestamp 值
抛出:
IllegalArgumentException - 如果给定的参数不具有 yyyy-mm-dd hh:mm:ss.fffffffff 格式
--------------------------------------------------------------------------------------------
Timestamp t1 = Timestamp.valueOf("2008-07-30 00:00:00");
Timestamp t2 = Timestamp.valueOf("2006-08-10 00:00:00");
long gapTime = t1.getTime() - t2.getTime();
System.out.println(gapTime);
long gapDay = gapTime / (24 * 3600 * 1000);
System.out.println("差距天数为:" + gapDay);
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;public class TranDate {
@SuppressWarnings("deprecation")
public static void main(String[]args){
SimpleDateFormat df=new SimpleDateFormat("yyyyMMdd");
String s="20070730";
Date dt = null;
try {
dt = df.parse(s);
} catch (ParseException e) {
e.printStackTrace();
}
Calendar c1 = Calendar.getInstance();
c1.setTime(dt);
Calendar c2 = Calendar.getInstance();
System.out.println(c1.getTime().toLocaleString());
System.out.println(c2.getTime().toLocaleString());
long t1=c1.getTimeInMillis();
long t2=c2.getTimeInMillis();
System.out.print((t2-t1)/(1000*60*60*24));
}}
public static Timestamp valueOf(String s)将使用 JDBC 时间戳转义格式的 String 对象转换为 Timestamp 值。 参数:
s - 使用 yyyy-mm-dd hh:mm:ss.fffffffff 格式的时间戳
返回:
相应的 Timestamp 值
抛出:
IllegalArgumentException - 如果给定的参数不具有 yyyy-mm-dd hh:mm:ss.fffffffff 格式
--------------------------------------------------------------------------------------------
Timestamp t1 = Timestamp.valueOf("2008-07-30 00:00:00");
Timestamp t2 = Timestamp.valueOf("2006-08-10 00:00:00");
long gapTime = t1.getTime() - t2.getTime();
System.out.println(gapTime);
long gapDay = gapTime / (24 * 3600 * 1000);
System.out.println("差距天数为:" + gapDay);
Calendar calendar=Calendar.getInstance();
calendar.set(calendar.get(calendar.YEAR),calendar.get(calendar.MONTH)+1,calendar.get(calendar.DATE));
long timeNow=calendar.getTimeInMillis();
calendar.set(2007, 07, 30);
long timeOld=calendar.getTimeInMillis();
System.out.println((timeNow-timeOld)/(60*60*24*1000));[/align]