package com.excel; 
 
import java.text.SimpleDateFormat; 
import java.util.Calendar; 
import java.util.Date; 
 
public class Test { 
	public static void main(String[] args) throws Exception { 
		Calendar now = Calendar.getInstance(); 
		System.out.println("年: " + now.get(Calendar.YEAR)); 
		System.out.println("月: " + (now.get(Calendar.MONTH) + 1) + ""); 
		System.out.println("日: " + now.get(Calendar.DAY_OF_MONTH)); 
		System.out.println("时: " + now.get(Calendar.HOUR_OF_DAY)); 
		System.out.println("分: " + now.get(Calendar.MINUTE)); 
		System.out.println("秒: " + now.get(Calendar.SECOND)); 
		System.out.println("当前时间毫秒数:" + now.getTimeInMillis()); 
		System.out.println(now.getTime()); 
 
		Date date = new Date(); 
		System.out.println(date); 
 
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
		String dateNowStr = sdf.format(date); 
		System.out.println("格式化后的日期:" + dateNowStr); 
 
		String str = "2012-1-13 17:26:33"; // 要跟上面sdf定义的格式一样 
		Date today = sdf.parse(str); 
		System.out.println("字符串转成日期:" + today); 
	} 
} 

获取某个日期是星期几

SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); 
		Calendar c = Calendar.getInstance(); 
		c.setTime(format.parse("2014-12-8")); 
		int dayForWeek = 0; 
		if (c.get(Calendar.DAY_OF_WEEK) == 1) { 
			dayForWeek = 7; 
		} else { 
			dayForWeek = c.get(Calendar.DAY_OF_WEEK) - 1; 
		} 
		System.out.println(dayForWeek);


发布评论

分享到:

IT虾米网

微信公众号号:IT虾米 (左侧二维码扫一扫)欢迎添加!

JAVA图片压缩详解
你是第一个吃螃蟹的人
发表评论

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。