Java中Calendar的一些簡單操作 及 JS中獲取當前時間前一天時間(js中時間Date操作)

1獲取當前月份年份

import java.util.Calendar;  
public class Main {  
  public static void main(String[] args) {  
   Calendar now = Calendar.getInstance(); 
   System.out.println("Current Year is : " + now.get(Calendar.YEAR)); 
  // month start from 0 to 11 
   System.out.println("Current Month is : " + (now.get(Calendar.MONTH) + 1)); 
   System.out.println("Current Date is : " + now.get(Calendar.DATE)); 
  } 
}  


2獲取前一天前一月的日期

Calendar calendar = Calendar.getInstance();//此時打印它獲取的是系統當前時間  
calendar.add(Calendar.DATE, -1);    //得到前一天  
String  yestedayDate = new SimpleDateFormat("yyyy-MM-dd").format(cal.getTime()); 
    calendar.add(Calendar.MONTH, -1);    //得到前一個月 
       int year = calendar.get(Calendar.YEAR);  
      int month = calendar.get(Calendar.MONTH)+1; //輸出前一月的時候要記得加1  



3字符串轉爲日期格式

String date = "2010-02-01 23:59:59"; 
SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");  
try {  
Date d = sf.parse(date); 
System.out.println(sf.format(d));  
} catch (ParseException e) { 
e.printStackTrace();  
} 


4完整的一個例子

import   java.util.Date;    
import   java.util.Calendar;      
import   java.text.SimpleDateFormat;     
public   class   TestDate{    
public   static   void   main(String[]   args){    
Date   now   =   new   Date();    
SimpleDateFormat   dateFormat   =   new   SimpleDateFormat("yyyy/MM/dd   HH:mm:ss");//可以方便地修改日期格式    
String   hehe   =   dateFormat.format(   now   );    
System.out.println(hehe);      
Calendar   c   =   Calendar.getInstance();//可以對每個時間域單獨修改    
int   year   =   c.get(Calendar.YEAR);    
int   month   =   c.get(Calendar.MONTH);    
int   date   =   c.get(Calendar.DATE);    
int   hour   =   c.get(Calendar.HOUR_OF_DAY);    
int   minute   =   c.get(Calendar.MINUTE);    
int   second   =   c.get(Calendar.SECOND);    
System.out.println(year   +   "/"   +   month   +   "/"   +   date   +   "   "   +hour   + 
  ":"   +minute   +   ":"   +   second);    
}    
}  



 

 

JS中獲取當前時間點前一天時間

  var date=new Date();
  var dat_year=date.getYear();
  var dat_month=date.getMonth();
  var dat_day=date.getDay();
	var dat_d=date.getDate();
//	alert(dat_d);
	var uom=new Date(dat_year,dat_month,dat_d);
//	alert(uom);
// 	alert(date);
	uom.setDate(uom.getDate()-1);
//	alert(uom);
	var new_dd=uom.getDate(); 
	var new_mm=uom.getMonth();
	var new_yy=uom.getFullYear();
	

JS實現將字符串轉爲Date類型的數據

var str="2012-09-20";
	var dat = new Date(Date.parse(str.replace(/-/g,"/")));
	var month=dat.getMonth();
	alert(month);

例子2

var s = "2012/08/06";
	var dateString = new Date(Date.parse(s));
	alert(dateString.getMonth());

 例子3

var s = "2012/08/06 12:02:03";
	var dateString = new Date(Date.parse(s));
	alert(dateString.getMonth());
	var t=dateString.toLocaleString();
	var st=dateString.toLocaleTimeString();
	alert(st);


Date類型常用方法

date.getYear();        //獲取當前年份(2位)   
date.getFullYear();    //獲取完整的年份(4位,1970-????)   
date.getMonth();       //獲取當前月份(0-11,0代表1月)   
date.getDate();        //獲取當前日(1-31)   
date.getDay();         //獲取當前星期X(0-6,0代表星期天)   
date.getTime();        //獲取當前時間(從1970.1.1開始的毫秒數)   
date.getHours();       //獲取當前小時數(0-23)   
date.getMinutes();     //獲取當前分鐘數(0-59)   
date.getSeconds();     //獲取當前秒數(0-59)   
date.getMilliseconds();    //獲取當前毫秒數(0-999)   
date.toLocaleDateString();     //獲取當前日期   
var mytime=date.toLocaleTimeString();     //獲取當前時間   
date.toLocaleString( );        //獲取日期與時間  即年月日 時分秒


 

 近日在網上看到了牛人

一句代碼

 

 var today = new Date();
alert([
    '  今天的日期爲:' + today.toLocaleString(),
    '七天前的日期爲:' + new Date((+today) - 7*24*60*60*1000).toLocaleString()
  ].join('\n'));


 這是計算當前時間之前7天的時間點的代碼 so easy。。。。

 上一個例子吧

	 var s="2012/1/1 12:28:15";
	 var date=new Date(Date.parse(s));
	 alert(new Date((+date)-24*3600*1000).toLocaleString());

這樣就能求的給定時間的前一天時間 這應該是最簡單的方法了

(某一天用到了 貼點代碼過來)

var date = new Date(Date.parse("1989/1/10 10:12:13"))
	
	alert(date.getFullYear()+"年"+(date.getMonth()+1)+"月"+date.getDate()+"日"+
		date.getHours()+"時"+date.getMinutes()+"分"+date.getSeconds()+"秒"
	);
	// 結果 1989年1月10日10時12分13秒
	var sj = new Date(1888,1,10,12,12,12);
	alert(sj.getFullYear()+"年"+sj.getMonth()+"月"+sj.getDate()+"日"+
		sj.getHours()+"時"+sj.getMinutes()+"分"+sj.getSeconds()+"秒"
	);
	//結果  1988年1月10日12時12分12秒


 


 

 順便我又用Java寫了點 上代碼

Calendar c = Calendar.getInstance();
			long l=c.getTimeInMillis();
			long res=l-24*3600*1000;
		c.setTimeInMillis(res);	
	String s = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(res);
			System.out.println(s);
	String st="2012-12-01 12:12:31";	
		SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date d = sf.parse(st);//要加try catch  我直接throws Exception了
		long t = d.getTime();
		long p=t-24*3600*1000;
		String test = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(p);
			System.out.println(test);


 

 


JS的內部機制應該就是 將字符串轉成 1234/12/12 的形式然後轉成Date類型 具體不是很清楚 找到後增加

 

 

 

 

 

 


 

發佈了91 篇原創文章 · 獲贊 44 · 訪問量 57萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章