Java獲取當前時間年月日、時間格式化打印、字符串轉日期


package com.sysc.simple;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtils {
	
	public static void show() throws ParseException {
		//獲取當前日期
		Calendar now = Calendar.getInstance();  
		int year = now.get(Calendar.YEAR);
		int month = now.get(Calendar.MONTH) + 1;//注意月份
		int day = now.get(Calendar.DAY_OF_MONTH);
		int hour = now.get(Calendar.HOUR_OF_DAY);
		int minute = now.get(Calendar.MINUTE);
		int second = now.get(Calendar.SECOND);
		long millis = now.getTimeInMillis();
        System.out.println(now.getTime());  
        System.out.println(year + month + day + hour + minute + second + millis);
  
        //日期格式化打印
        Date d = new Date();  
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
        String dateNowStr = sdf.format(d);  
        System.out.println("格式化后的日期:" + dateNowStr);  
        
        //字符串解析為日期
        String str = "2014-9-1 13:08:28";  
        Date today = sdf.parse(str);  
        System.out.println("字符串轉成日期:" + today);  
	}

}

  


免責聲明!

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



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