JAVA 獲取當前月的初始時間的時間戳
- public static long getMonthFirstDay() {
- Calendar calendar = Calendar.getInstance();// 獲取當前日期
- calendar.add(Calendar.MONTH, 0);
- calendar.set(Calendar.DAY_OF_MONTH, 1);// 設置為1號,當前日期既為本月第一天
- calendar.set(Calendar.HOUR_OF_DAY, 0);
- calendar.set(Calendar.MINUTE, 0);
- calendar.set(Calendar.SECOND, 0);
- System.out.println(calendar.getTimeInMillis());
- return calendar.getTimeInMillis();
- }
獲取當前時間戳
//方法 一 System.currentTimeMillis(); //方法 二 Calendar.getInstance().getTimeInMillis(); //方法 三 new Date().getTime();
Calendar.getInstance().getTimeInMillis() 這種方式速度最慢,這是因為Canlendar要處理時區問題會耗費較多的時間。
在開發過程中,通常很多人都習慣使用new Date()來獲取當前時間,使用起來也比較方便,同時還可以獲取與當前時間有關的各方面信息,例如獲取小時,分鍾等等,而且還可以格式化輸出,包含的信息是比較豐富的。但是有些時候或許你並不需要獲取那么多信息,你只需要關心它返回的毫秒數就行了,例如getTime()。為了獲取這個時間戳,很多人也喜歡使用new Date().getTime()去獲取,咋一看沒什么問題,但其實沒這個必要。
其實看一下java的源碼就知道了:
public Date() { this(System.currentTimeMillis()); }
已經很明顯了,new Date()所做的事情其實就是調用了System.currentTimeMillis()。如果僅僅是需要或者毫秒數,那么完全可以使用System.currentTimeMillis()去代替new Date(),效率上會高一點。況且很多人喜歡在同一個方法里面多次使用new Date(),通常性能就是這樣一點一點地消耗掉,這里其實可以聲明一個引用。
獲取當前時間
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//設置日期格式
String date = df.format(new Date());// new Date()為獲取當前系統時間,也可使用當前時間戳
我們看一下JAVA獲得當前時間的幾種方法
package com.xjp.common.util;
import java.sql.Timestamp;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Date;
import com.ttsoft.framework.util.DateUtil;
/**
* Title: 時間獲取
* Description: 當前時間
* Company:
* @author jiq
* @version 1.0
*/
public class XJPDateUtil extends DateUtil {
public static final String[] months = { "一月", "二月", "三月", "四月", "五月", "六月",
"七月", "八月", "九月", "十月", "十一月", "十二月", };
public static final String[] quarters = { "一季度", "二季度", "三季度", "四季度" };
public XJPDateUtil() {
}
/**
* 獲取日期字符串。
*
*
* 日期字符串格式: yyyyMMdd
* 其中:
* yyyy 表示4位年。
* MM 表示2位月。
* dd 表示2位日。
*
*
* @return String "yyyyMMdd"格式的日期字符串。
*/
public static String getDate() {
SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
return formatter.format(new Date());
}
/**
* 獲取當前年度字符串。
*
*
* 日期字符串格式: yyyy
* 其中:
* yyyy 表示4位年。
*
*
* @return String "yyyy"格式的當前年度字符串。
*/
public static String getNowYear() {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy");
return formatter.format(new Date());
}
/**
* 獲取當前月度字符串。
*
*
* 日期字符串格式: MM
* 其中:
* MM 表示4位年。
*
*
* @return String "yyyy"格式的當前月度字符串。
*/
public static String getNowMonth() {
SimpleDateFormat formatter = new SimpleDateFormat("MM");
return formatter.format(new Date());
}
/**
* 獲取當前月度字符串。
*
*
* 日期字符串格式: dd
* 其中:
* dd 表示4位年。
*
*
* @return String "yyyy"格式的當前月度字符串。
*/
public static String getNowDay() {
SimpleDateFormat formatter = new SimpleDateFormat("dd");
return formatter.format(new Date());
}
/**
* 獲取日期字符串。
*
*
* 日期字符串格式: yyyyMMdd
* 其中:
* yyyy 表示4位年。
* MM 表示2位月。
* dd 表示2位日。
*
*
* @param date
* 需要轉化的日期。
* @return String "yyyyMMdd"格式的日期字符串。
*/
public static String getDate(Date date) {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
return formatter.format(date);
}
/**
* 獲取日期字符串。
*
*
* 日期字符串格式: yyyyMMdd
* 其中:
* yyyy 表示4位年。
* MM 表示2位月。
* dd 表示2位日。
*
*
* @param date
* 需要轉化的日期。
* @return String "yyyyMMdd"格式的日期字符串。
*/
/**
* 將指定的日期字符串轉化為日期對象
*
* @param dateStr
* 日期字符串
* @return java.util.Date
* @roseuid 3F39FE450385
*/
public static Date getDate(String dateStr) {
if (XJPTypeChecker.isDate(dateStr)) { // 日期型
SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd");
try {
java.util.Date date = df.parse(dateStr);
return date;
} catch (Exception ex) {
Logger.write("日期格式不符合或者日期為空!請檢查!");
return null;
} // end try - catch
} else if (XJPTypeChecker.isDatetime(dateStr)) { // 日期時間型
SimpleDateFormat df = new SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss.SSS");
try {
java.util.Date date = df.parse(dateStr);
return date;
} catch (Exception ex) {
return null;
} // end try - catch
} // end if
return null;
}
/**
* 獲取日期字符串。
*
*
* 日期字符串格式: yyyy-MM-dd
* 其中:
* yyyy 表示4位年。
* MM 表示2位月。
* dd 表示2位日。
*
*
* @return String "yyyy-MM-dd"格式的日期字符串。
*/
public static String getHyphenDate() {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
return formatter.format(new Date());
}
/**
* 獲取日期字符串。
*
*
* 日期字符串格式: yyyy-MM-dd
* 其中:
* yyyy 表示4位年。
* MM 表示2位月。
* dd 表示2位日。
*
*
* @param date
* 需要轉化的日期。
* @return String "yyyy-MM-dd"格式的日期字符串。
*/
public static String getHyphenDate(Date date) {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
return formatter.format(date);
}
/**
* 將"yyyyMMdd"格式的日期字符串轉換為日期對象。
*
* @param source
* 日期字符串。
* @return Date 日期對象。
*/
public static Date parsePlainDate(String source) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
return sdf.parse(source, new ParsePosition(0));
}
/**
* 將“yyyy-MM-dd”格式的日期字符串轉換為日期對象。
*
* @param source
* 日期字符串。
* @return Date 日期對象。
*/
public static Date parseHyphenDate(String source) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
return sdf.parse(source, new ParsePosition(0));
}
/**
* 將指定格式的日期字符串轉換為日期對象。
*
* @param source
* 日期字符串。
* @param pattern
* 模式。
* @return Date 日期對象。
*/
public static Date parseDate(String source, String pattern) {
SimpleDateFormat sdf = new SimpleDateFormat(pattern);
return sdf.parse(source, new ParsePosition(0));
}
/**
* 將“yyyy-MM-dd”格式的日期字符串轉換為“yyyyMMdd”格式的日期字符串。
*
* @param source
* 日期字符串。
* @return String "yyyymmdd"格式的日期字符串。
*/
public static String toPlainDate(String source) {
Date date = parseHyphenDate(source);
SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
return formatter.format(date);
}
/**
* 將“yyyyMMdd”格式的日期字符串轉換為“yyyy-MM-dd”格式的日期字符串。
*
* @param source
* 日期字符串。
* @return String "yyyy-MM-dd"格式的日期字符串。
*/
public static String toHyphenDate(String source) {
Date date = parsePlainDate(source);
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
return formatter.format(date);
}
/**
* 獲取時間戳,將日期對象轉換為時間戳類型。
*
* @param date
* 日期對象
* @return Timestamp 時間戳
*/
public static Timestamp getTimestamp(Date date) {
return new Timestamp(date.getTime());
}
/**
* 獲取時間戳,將當前日期轉換為時間戳類型。
*
* @return Timestamp 時間戳
*/
public static Timestamp getTimestamp() {
return new Timestamp(new Date().getTime());
}
/**
* 將“yyyyMMdd”格式的日期字符串轉換為Timestamp類型的對象。
*
* @param source
* 日期字符串
* @return Timestamp 時間戳
*/
public static Timestamp parseTimestamp(String source) {
Date date = parsePlainDate(source);
return getTimestamp(date);
}
/**
* 獲得年度周期
* Example:
* XJPDateUtil.getPeriodYear("20040229" , -1);
* XJPDateUtil.getPeriodYear("20040228" , -1);
* XJPDateUtil.getPeriodYear("20020230" , 2);
*
* @param source
* 時間串
* @param yearPeriod
* 年度周期 -1代表本時間的上一年度,以次類推。
* @return String 時間。
*/
public static String getPeriodYear(String source, int yearPeriod) {
int p = Integer.parseInt(source.substring(0, 4)) + yearPeriod;
String newYear = String.valueOf(p)
+ source.substring(4, source.length());
Date date = parsePlainDate(newYear);
String s = getDate(date);
int ny = Integer.parseInt(s);
int sy = Integer.parseInt(newYear);
while (ny > sy) {
sy--;
ny = Integer.parseInt(getDate(parsePlainDate(String.valueOf(sy))));
}
return String.valueOf(sy);
}
/**
* 獲取當前日期和時間
*
* @return String
*/
public static String getCurrentDateStr() {
Date date = new Date();
String str = null;
SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd HH:mm:ss");
str = df.format(date);
return str;
}
/**
* 日期相加
*
* @param day
* 天數
* @return 返回相加后的日期
*/
public static String addDate(int day) {
java.util.Calendar c = java.util.Calendar.getInstance();
c.setTimeInMillis(System.currentTimeMillis() + ((long) day) * 24 * 3600
* 1000);
SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd HH:mm:ss");
return df.format(c.getTime());
}
/**
* 返回毫秒
*
* @param date
* 日期
* @return 返回毫秒
*/
public static long getMillis(java.util.Date date) {
java.util.Calendar c = java.util.Calendar.getInstance();
c.setTime(date);
return c.getTimeInMillis();
}
/**
* 獲取當前日期和時間
* @param format 日期格式 例:yyyy-MM-dd hh:mm
* @return String
*/
public static String getNowDate(String format) {
Date date = new Date();
String str = null;
SimpleDateFormat df = new SimpleDateFormat(format);
str = df.format(date);
return str;
}
/**
* 將strmon的日期減小一個月
* @param mon
* @return
*/
public static String getReduceMonDate(String strmon) {
if (strmon != null && !strmon.equals("")) {
Date mon = parseHyphenDate(strmon);
mon.setMonth(mon.getMonth() - 1);
return getHyphenDate(mon);
} else {
return "";
}
}
public static String getTimeStr(String dateStr){
Date date=getDate(dateStr);
String str = null;
SimpleDateFormat df = new SimpleDateFormat("HH:mm:ss");
str = df.format(date);
return str;
}
public static String getTimeStr(){
String str="";
SimpleDateFormat df = new SimpleDateFormat("HH:mm:ss");
str = df.format(new Date());
return str;
}
}