得到java时间戳 用于数据库中按时间戳查询数据
得到java时间戳 用于数据库中按时间戳查询数据
包括:今天、昨天、本周、本月、本年、选定的时间段
package test;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
public class DateTest {
public static long FromYear(){
Calendar nowDate = new java.util.GregorianCalendar();
nowDate.set(Calendar.HOUR_OF_DAY,0);
nowDate.set(Calendar.MINUTE,0);
nowDate.set(Calendar.SECOND,0);
nowDate.set(Calendar.MILLISECOND,0);
nowDate.set(Calendar.DAY_OF_MONTH,1);
nowDate.set(Calendar.MONTH,0);
return? nowDate.getTimeInMillis()/1000;
}
public static long ToYear(){
Calendar nowDate= new java.util.GregorianCalendar();
nowDate.set(Calendar.HOUR_OF_DAY,0);
nowDate.set(Calendar.MINUTE,0);
nowDate.set(Calendar.SECOND,0);
nowDate.set(Calendar.MILLISECOND,0);
nowDate.set(Calendar.DAY_OF_MONTH,1);
nowDate.set(Calendar.MONTH,12);
return? nowDate.getTimeInMillis()/1000;
}
public static long FromMonth(){
Calendar nowDate = new java.util.GregorianCalendar();
nowDate.set(Calendar.HOUR_OF_DAY,0);
nowDate.set(Calendar.MINUTE,0);
nowDate.set(Calendar.SECOND,0);
nowDate.set(Calendar.MILLISECOND,0);
nowDate.set(Calendar.DAY_OF_MONTH,1);
return? nowDate.getTimeInMillis()/1000;
}
public static long ToMonth(){
Calendar nowDate = new java.util.GregorianCalendar();
nowDate.set(Calendar.HOUR_OF_DAY,0);
nowDate.set(Calendar.MINUTE,0);
nowDate.set(Calendar.SECOND,0);
nowDate.set(Calendar.MILLISECOND,0);
nowDate.set(Calendar.DAY_OF_MONTH,1);
nowDate.add(Calendar.MONTH,1);
return? nowDate.getTimeInMillis()/1000;
}
public static long FromWeek(){
Calendar nowDate = new java.util.GregorianCalendar();
nowDate.set(Calendar.HOUR_OF_DAY,0);
nowDate.set(Calendar.MINUTE,0);
nowDate.set(Calendar.SECOND,0);
nowDate.set(Calendar.MILLISECOND,0);
nowDate.set(Calendar.DAY_OF_WEEK,Calendar.MONDAY);
return? nowDate.getTimeInMillis()/1000;
}
public static long ToWeek(){
Calendar nowDate = new java.util.GregorianCalendar();
nowDate.set(Calendar.HOUR_OF_DAY,0);
nowDate.set(Calendar.MINUTE,0);
nowDate.set(Calendar.SECOND,0);
nowDate.set(Calendar.MILLISECOND,0);
nowDate.set(Calendar.DAY_OF_WEEK,Calendar.MONDAY);
nowDate.add(Calendar.WEEK_OF_MONTH,1);
return? nowDate.getTimeInMillis()/1000;
}
public static long FromYesterday(){
Calendar nowDate = new java.util.GregorianCalendar();
nowDate.set(Calendar.HOUR_OF_DAY,0);
nowDate.set(Calendar.MINUTE,0);
nowDate.set(Calendar.SECOND,0);
nowDate.set(Calendar.MILLISECOND,0);
nowDate.add(Calendar.DAY_OF_MONTH,-1);
return? nowDate.getTimeInMillis()/1000;
}
public static long ToYesterday(){
Calendar nowDate = new java.util.GregorianCalendar();
nowDate.set(Calendar.HOUR_OF_DAY,0);
nowDate.set(Calendar.MINUTE,0);
nowDate.set(Calendar.SECOND,0);
nowDate.set(Calendar.MILLISECOND,0);
return? nowDate.getTimeInMillis()/1000;
}
public static long FromToday(){
Calendar nowDate = new java.util.GregorianCalendar();
nowDate.set(Calendar.HOUR_OF_DAY,0);
nowDate.set(Calendar.MINUTE,0);
nowDate.set(Calendar.SECOND,0);
nowDate.set(Calendar.MILLISECOND,0);
return? nowDate.getTimeInMillis()/1000;
}
public static long ToToday(){
Calendar nowDate = new java.util.GregorianCalendar();
nowDate.set(Calendar.HOUR_OF_DAY,0);
nowDate.set(Calendar.MINUTE,0);
nowDate.set(Calendar.SECOND,0);
nowDate.set(Calendar.MILLISECOND,0);
nowDate.add(Calendar.DAY_OF_MONTH,1);
return? nowDate.getTimeInMillis()/1000;
}
/**startDate 格式 2009-02-03
* startTime 格式 12:20
* */
public static long FromTime(String startDate,String startTime){
Calendar nowDate = new java.util.GregorianCalendar();
l