Java实现判断某一时间是上午下午
//现在的时间
Date startTime= new Date();
//时间格式转换器
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
//用StringBuilder进行字符串拼接
StrBuilder builder = new StrBuilder();
//用DateUtils类中的getHour获取时间的小时数
//DateUtils是一个网上下载的工具类,下面我附上代码
if (DateUtils.getHour(startTime)>12) {
builder.append(format.format(startTime)+ "12:00:00 AND "+format.format(startTime)+" 23:59:59 AND");
}else {
builder.append(format.format(startTime)+" AND "+format.format(startTime)+" 11:59:59 ");
}
return builder;
DateUtils工具类
import org.apache.commons.lang3.time.DateFormatUtils;
import java.net.URL;
import java.net.URLConnection;
import java.text.DateFormat;
import java.text.ParseException;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
public class DateUtils extends org.apache.commons.lang3.time.DateUtils {
private static String[] parsePatterns = { "yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm", "yyyy-MM", "yyyy/MM/dd", "yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd HH:mm", "yyyy/MM", "yyyy.MM.dd",
"yyyy.MM.dd HH:mm:ss", "yyyy.MM.dd HH:mm", "yyyy.MM" };
/**
* 得到当前日期字符串 格式(yyyy-MM-dd)
*/
public static String getDate() {
return getDate("yyyy-MM-dd");
}
/**
* 得到当前日期字符串 格式(yyyy-MM-dd) pattern可以为:"yyyy-MM-dd" "HH:mm:ss" "E"
*/
public static String getDate(String pattern) {
return DateFormatUtils.format(new Date(), pattern);
}
/**
* 得到日期字符串 默认格式(yyyy-MM-dd) pattern可以为:"yyyy-MM-dd" "HH:mm:ss" "E"
*/
public static String formatDate(Date date, Object... pattern) {
String formatDate = null;
if (pattern != null && pattern.length > 0) {
formatDate = DateFormatUtils.format(date, pattern[0].toString());
} else {
formatDate = DateFormatUtils.format(date, "yyyy-MM-dd");
}
return formatDate;
}
/**
* 得到日期时间字符串,转换格式(yyyy-MM-dd HH:mm:ss)
*/
public static String formatDateTime(Date date) {
return formatDate(date, "yyyy-MM-dd HH:mm:ss");
}
/**
* 得到当前时间字符串 格式(HH:mm:ss)
*/
public static String getTime() {
return formatDate(new Date(), "HH:mm:ss");
}
/**
* 得到当前日期和时间字符串 格式(yyyy-MM-dd HH:mm:ss)
*/
public static String getDateTime() {
return formatDate(new Date(), "yyyy-MM-dd HH:mm:ss");
}
/**
* 得到当前年份字符串 格式(yyyy)
*/
public static String getYear() {
return formatDate(new Date(), "yyyy");
}
/**
* 得到当前月份字符串 格式(MM)
*/
public static String getMonth() {
return formatDate(new Date(), "MM");
}
/**
* 得到当天字符串 格式(dd)
*/
public static String getDay() {
return formatDate(new Date(), "dd");
}
/**
* 得到当前星期字符串 格式(E)星期几
*/
public static String getWeek() {
return formatDate(new Date(), "E");
}
/**
* 获取时间的分钟
* @param date
* @return
*/
public static Integer getMinute(Date date){
if(date==null)
{
return null;
}
return getCalendar(date).get(Calendar.MINUTE);
}
/**
* 获取时间的小时
* @param date
* @return
*/
public static Integer getHour(Date date){
if(date==null)
{
return null;
}
return getCalendar(date).get(Calendar.HOUR_OF_DAY);
}
/**
* 获取时间是几号
* @param date
* @return
*/
public static Integer getDay(Date date){
if(date==null)
{
return null;
}
return getCalendar(date).get(Calendar.DAY_OF_MONTH);
}
/**
* 获取时间的月份
* @param date
* @return
*/
public static Integer getMonth(Date date){
if(date==null)
{
return null;
}
return getCalendar(date).get(Calendar.MONTH)+1;
}
/**
* 获取时间的年份
* @param date
* @return
*/
public static Integer getYear(Date date){
if(date==null)
{
return null;
}
return getCalendar(date).get(Calendar.YEAR);
}
/**
* 日期型字符串转化为日期 格式 { "yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm", "yyyy/MM/dd", "yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd HH:mm", "yyyy.MM.dd", "yyyy.MM.dd HH:mm:ss", "yyyy.MM.dd HH:mm" }
*/
public static Date parseDate(Object str) {
if (str == null) {
return null;
}
try {
return parseDate(str.toString(), parsePatterns);
} catch (ParseException e) {
return null;
}
}
/**
* 获取过去的天数
*
* @param date
* @return
*/
public static long pastDays(Date date) {
long t = new Date().getTime() - date.getTime();
return t / (24 * 60 * 60 * 1000);
}
/**
* 获取过去的小时
*
* @param date
* @return
*/
public static long pastHour(Date date) {
long t = new Date().getTime() - date.getTime();
return t / (60 * 60 * 1000);
}
/**
* 获取过去的分钟
*
* @param date
* @return
*/
public static long pastMinutes(Date date) {
long t = new Date().getTime() - date.getTime();
return t / (60 * 1000);
}
/**
* 转换为时间(天,时:分:秒.毫秒)
*
* @param timeMillis
* @return
*/
public static String formatDateTime(long timeMillis) {
long day = timeMillis / (24 * 60 * 60 * 1000);
long hour = (timeMillis / (60 * 60 * 1000) - day * 24);
long min = ((timeMillis / (60 * 1000)) - day * 24 * 60 - hour * 60);
long s = (timeMillis / 1000 - day * 24 * 60 * 60 - hour * 60 * 60 - min * 60);
long sss = (timeMillis - day * 24 * 60 * 60 * 1000 - hour * 60 * 60 * 1000 - min * 60 * 1000 - s * 1000);
return (day > 0 ? day + "," : "") + hour + ":" + min + ":" + s + "." + sss;
}
/**
* 获取两个日期之间的天数
*
* @param before
* @param after
* @return
*/
public static double getDistanceOfTwoDate(Date before, Date after) {
long beforeTime = before.getTime();
long afterTime = after.getTime();
return (afterTime - beforeTime) / (1000 * 60 * 60 * 24);
}
public static Date getBJDate() {
try {
URL url = new URL("http://www.bjtime.cn");
// 取得资源对象
URLConnection uc = url.openConnection();// 生成连接对象
uc.connect(); // 发出连接
long ld = uc.getDate(); // 取得网站日期时间
Date date = new Date(ld); // 转换为标准时间对象
return date;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* 获取北京时间 yyyy-MM-dd HH:mm
* @Description:
* @date 2016年1月14日
*/
public static String getBJDateStr() {
return formatDate(getBJDate(), "yyyy-MM-dd HH:mm");
}
public static Calendar getCalendar(Date date){
if(date==null)
{
return null;
}
DateFormat df = DateFormat.getDateInstance();
df.format(date);
return df.getCalendar();
}
/**
* 加/减几年的时间
* @param sdate
* @param i
* @return
*/
public static Date getDateAfterYear(Date sdate,Integer i) {
if(sdate==null){
return null;
}
Calendar cal = Calendar.getInstance();
cal.setTime(sdate);
cal.add(GregorianCalendar.YEAR, i);
return cal.getTime();
}
/**
* 加/减几个月的时间
* @param sdate
* @param i
* @return
*/
public static Date getDateAfterMonth(Date sdate,Integer i) {
if(sdate==null){
return null;
}
Calendar cal = Calendar.getInstance();
cal.setTime(sdate);
cal.add(GregorianCalendar.MONTH, i);
return cal.getTime();
}
/**
* 加/减几天的时间
* @param sdate
* @param i
* @return
*/
public static Date getDateAfterDay(Date sdate,Integer i) {
if(sdate==null){
return null;
}
Calendar cal = Calendar.getInstance();
cal.setTime(sdate);
cal.add(GregorianCalendar.DATE, i);
return cal.getTime();
}
/**
* 加/减几小时的时间
* @param sdate
* @param i
* @return
*/
public static Date getDateAfterHour(Date sdate,Integer i) {
if(sdate==null){
return null;
}
Calendar cal = Calendar.getInstance();
cal.setTime(sdate);
cal.add(GregorianCalendar.HOUR, i);
return cal.getTime();
}
/**
* 加/减多少分钟的时间
* @param sdate
* @param i
* @return
*/
public static Date getDateAfterMinute(Date sdate,Integer i) {
if(sdate==null){
return null;
}
Calendar cal = Calendar.getInstance();
cal.setTime(sdate);
cal.add(GregorianCalendar.MINUTE, i);
return cal.getTime();
}
}