java實現獲取當前年月日 小時 分鍾 秒 毫秒


java代碼實現如下

    /**
     * 英文簡寫(默認)如:2010-12-01
     */
    public static String FORMAT_SHORT = "yyyy-MM-dd";
     
    /**
     * 英文全稱  如:2010-12-01 23:15:06
     */
    public static String FORMAT_LONG = "yyyy-MM-dd HH:mm:ss";
     
    /**
     * 精確到毫秒的完整時間    如:yyyy-MM-dd HH:mm:ss.S
     */
    public static String FORMAT_FULL = "yyyy-MM-dd HH:mm:ss.S";
     
    /**
     * 中文簡寫  如:2010年12月01日
     */
    public static String FORMAT_SHORT_CN = "yyyy年MM月dd";
     
    /**
     * 中文全稱  如:2010年12月01日  23時15分06秒
     */
    public static String FORMAT_LONG_CN = "yyyy年MM月dd日  HH時mm分ss秒";
     
    /**
     * 精確到毫秒的完整中文時間
     */
    public static String FORMAT_FULL_CN = "yyyy年MM月dd日  HH時mm分ss秒SSS毫秒";
 
 
 
 
 
 
 
 
/**
   * 獲取時間戳
   */
public static String getTimeString() {
    SimpleDateFormat df = new SimpleDateFormat(FORMAT_FULL);
    Calendar calendar = Calendar.getInstance();
    return df.format(calendar.getTime());
}
 
/**
 * 獲取日期年份
 * @param date 日期
 * @return
 */
public static String getYear(Date date) {
    return format(date).substring(04);
}
/**
 * 功能描述:返回月
 *
 * @param date
 *            Date 日期
 * @return 返回月份
 */
public static int getMonth(Date date) {
    calendar = Calendar.getInstance();
    calendar.setTime(date);
    return calendar.get(Calendar.MONTH) + 1;
}
 
/**
 * 功能描述:返回日
 *
 * @param date
 *            Date 日期
 * @return 返回日份
 */
public static int getDay(Date date) {
    calendar = Calendar.getInstance();
    calendar.setTime(date);
    return calendar.get(Calendar.DAY_OF_MONTH);
}
 
/**
 * 功能描述:返回小
 *
 * @param date
 *            日期
 * @return 返回小時
 */
public static int getHour(Date date) {
    calendar = Calendar.getInstance();
    calendar.setTime(date);
    return calendar.get(Calendar.HOUR_OF_DAY);
}
 
/**
 * 功能描述:返回分
 *
 * @param date
 *            日期
 * @return 返回分鍾
 */
public static int getMinute(Date date) {
    calendar = Calendar.getInstance();
    calendar.setTime(date);
    return calendar.get(Calendar.MINUTE);
}
 
/**
 * 返回秒鍾
 *
 * @param date
 *            Date 日期
 * @return 返回秒鍾
 */
public static int getSecond(Date date) {
    calendar = Calendar.getInstance();
    calendar.setTime(date);
    return calendar.get(Calendar.SECOND);
}
 
/**
 * 功能描述:返回毫
 *
 * @param date
 *            日期
 * @return 返回毫
 */
public static long getMillis(Date date) {
    calendar = Calendar.getInstance();
    calendar.setTime(date);
    return calendar.getTimeInMillis();
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM