1.输出当前时间(长整型)和两小时间以前的时间(长整型)
import java.text.SimpleDateFormat;
import java.util.Calendar;
public class dateShow {
public static void main(String[] args)
{
Calendar c= Calendar.getInstance();
SimpleDateFormat sf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println(sf.format(c.getTime()));
System.out.println(c.getTimeInMillis());
long d=c.getTimeInMillis()-2*60*60*1000;
System.out.println(sf.format(d));
System.out.println(d);
}
}
2.当前时间,前一个到前N个时间点
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
public class GetBeforeTime {
public static String getBeforeTime(String date){
DateFormat f = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date d = new Date(0);
try {
d = f.parse(date);
} catch (Exception e) {
e.printStackTrace();
}
long Time=(d.getTime());
long datet = Time-15*60* 1000;
String ss = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(datet);
return ss;
}
public static long getBeforeTimeLong(String date){
DateFormat f = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date d = new Date(0);
try {
d = f.parse(date);
} catch (Exception e) {
e.printStackTrace();
}
long Time=(d.getTime());
long datet = Time-15*60* 1000;
return datet;
}
public static void getBeforeTimeMore(String date){
DateFormat f = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date d = new Date(0);
try {
d = f.parse(date);
} catch (Exception e) {
e.printStackTrace();
}
long Time=(d.getTime());
for(int i=0;i<10;i++)
{
System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(Time-i*60*60* 1000)+" "+(Time-i*60*60* 1000));
}
}
public static void main(String[] args)
{
getBeforeTimeMore(" 2011-06-08 16:44:22");
}
}
3.长整型转化成具体的时间:
import java.text.SimpleDateFormat;
import java.util.Calendar;
public class LongChangeDate {
public static void main(String[] args)
{
LongChangeDate lc=new LongChangeDate();
lc.changedate1(1307384531315l);
}
public void changedate1(long strdate){
Calendar aa = Calendar.getInstance();
aa.setTimeInMillis(strdate);
SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println(" "+sf.format(aa.getTime()));
}
}