❶ java代碼中如何獲得當前時間
有兩種方法:
方法一:用java.util.Date類來實現,並結合java.text.DateFormat類來實現時間的格式化,看下面代碼:
import java.util.*;
import java.text.*;
//以下默認時間日期顯示方式都是漢語語言方式
//一般語言就默認漢語就可以了,時間日期的格式默認為MEDIUM風格,比如:2008-6-16 20:54:53
//以下顯示的日期時間都是再Date類的基礎上的來的,還可以利用Calendar類來實現見類TestDate2.java
public class TestDate {
public static void main(String[] args) {
Date now = new Date();
Calendar cal = Calendar.getInstance();
DateFormat d1 = DateFormat.getDateInstance(); //默認語言(漢語)下的默認風格(MEDIUM風格,比如:2008-6-16 20:54:53)
String str1 = d1.format(now);
DateFormat d2 = DateFormat.getDateTimeInstance();
String str2 = d2.format(now);
DateFormat d3 = DateFormat.getTimeInstance();
String str3 = d3.format(now);
DateFormat d4 = DateFormat.getInstance(); //使用SHORT風格顯示日期和時間
String str4 = d4.format(now);
DateFormat d5 = DateFormat.getDateTimeInstance(DateFormat.FULL,DateFormat.FULL); //顯示日期,周,時間(精確到秒)
String str5 = d5.format(now);
DateFormat d6 = DateFormat.getDateTimeInstance(DateFormat.LONG,DateFormat.LONG); //顯示日期。時間(精確到秒)
String str6 = d6.format(now);
DateFormat d7 = DateFormat.getDateTimeInstance(DateFormat.SHORT,DateFormat.SHORT); //顯示日期,時間(精確到分)
String str7 = d7.format(now);
DateFormat d8 = DateFormat.getDateTimeInstance(DateFormat.MEDIUM,DateFormat.MEDIUM); //顯示日期,時間(精確到分)
String str8 = d8.format(now);//與SHORT風格相比,這種方式最好用
System.out.println("用Date方式顯示時間: " + now);//此方法顯示的結果和Calendar.getInstance().getTime()一樣
System.out.println("用DateFormat.getDateInstance()格式化時間後為:" + str1);
System.out.println("用DateFormat.getDateTimeInstance()格式化時間後為:" + str2);
System.out.println("用DateFormat.getTimeInstance()格式化時間後為:" + str3);
System.out.println("用DateFormat.getInstance()格式化時間後為:" + str4);
System.out.println("用DateFormat.getDateTimeInstance(DateFormat.FULL,DateFormat.FULL)格式化時間後為:" + str5);
System.out.println("用DateFormat.getDateTimeInstance(DateFormat.LONG,DateFormat.LONG)格式化時間後為:" + str6);
System.out.println("用DateFormat.getDateTimeInstance(DateFormat.SHORT,DateFormat.SHORT)格式化時間後為:" + str7);
System.out.println("用DateFormat.getDateTimeInstance(DateFormat.MEDIUM,DateFormat.MEDIUM)格式化時間後為:" + str8);
}
}
運行結果:
用Date方式顯示時間: Mon Jun 16 20:54:53 CST 2008
用DateFormat.getDateInstance()格式化時間後為:2008-6-16
用DateFormat.getDateTimeInstance()格式化時間後為:2008-6-16 20:54:53
用DateFormat.getTimeInstance()格式化時間後為:20:54:53
用DateFormat.getInstance()格式化時間後為:08-6-16 下午8:54
用DateFormat.getDateTimeInstance(DateFormat.FULL,DateFormat.FULL)格式化時間後為
:2008年6月16日 星期一 下午08時54分53秒 CST
用DateFormat.getDateTimeInstance(DateFormat.LONG,DateFormat.LONG)格式化時間後為
:2008年6月16日 下午08時54分53秒
用DateFormat.getDateTimeInstance(DateFormat.SHORT,DateFormat.SHORT)格式化時間後
為:08-6-16 下午8:54
用DateFormat.getDateTimeInstance(DateFormat.MEDIUM,DateFormat.MEDIUM)格式化時間
後為:2008-6-16 20:54:53
方法二:用java.util.Calendar類來實現,看下面:
import java.util.*;
import java.text.*;
//以下是利用Calendar類來實現日期時間的,和Date類相比較比較簡單
public class TestDate2 {
public static void main(String[] args) {
Calendar ca = Calendar.getInstance();
int year = ca.get(Calendar.YEAR);//獲取年份
int month=ca.get(Calendar.MONTH);//獲取月份
int day=ca.get(Calendar.DATE);//獲取日
int minute=ca.get(Calendar.MINUTE);//分
int hour=ca.get(Calendar.HOUR);//小時
int second=ca.get(Calendar.SECOND);//秒
int WeekOfYear = ca.get(Calendar.DAY_OF_WEEK);
System.out.println("用Calendar.getInstance().getTime()方式顯示時間: " + ca.getTime());
System.out.println("用Calendar獲得日期是:" + year +"年"+ month +"月"+ day + "日");
System.out.println("用Calendar獲得時間是:" + hour +"時"+ minute +"分"+ second +"秒");
System.out.println(WeekOfYear);//顯示今天是一周的第幾天(我做的這個例子正好是周二,故結果顯示2,如果你再周6運行,那麼顯示6)
}
}
運行結果是:
用Calendar.getInstance().getTime()方式顯示時間: Mon Jun 16 21:54:21 CST 2008
用Calendar獲得日期是:2008年5月16日
用Calendar獲得時間是:9時54分21秒
2
總結:中的來說,方法二是最方便的,方法一顯得分笨拙,不過看個人喜歡了。
還有一種方法利用System.currentTimeMillis()也可以。
❷ 在LINUX下用C++編程,如何獲取系統當前的時間。
//方案— 優點:僅使用C標准庫;缺點:只能精確到秒級
#include <time.h>
#include <stdio.h>
int main( void )
{
time_t t = time(0);
char tmp[64];
strftime( tmp, sizeof(tmp), "%Y/%m/%d %X %A 本年第%j天 %z",localtime(&t) );
puts( tmp );
return 0;
}
size_t strftime(char *strDest, size_t maxsize, const char *format, const struct tm *timeptr);
根據格式字元串生成字元串。
struct tm *localtime(const time_t *timer);
取得當地時間,localtime獲取的結果由結構tm返回
返回的字元串可以依下列的格式而定:
%a 星期幾的縮寫。Eg:Tue
%A 星期幾的全名。 Eg: Tuesday
%b 月份名稱的縮寫。
%B 月份名稱的全名。
%c 本地端日期時間較佳表示字元串。
%d 用數字表示本月的第幾天 (范圍為 00 至 31)。日期
%H 用 24 小時制數字表示小時數 (范圍為 00 至 23)。
%I 用 12 小時制數字表示小時數 (范圍為 01 至 12)。
%j 以數字表示當年度的第幾天 (范圍為 001 至 366)。
%m 月份的數字 (范圍由 1 至 12)。
%M 分鍾。
%p 以 ''AM'' 或 ''PM'' 表示本地端時間。
%S 秒數。
%U 數字表示為本年度的第幾周,第一個星期由第一個周日開始。
%W 數字表示為本年度的第幾周,第一個星期由第一個周一開始。
%w 用數字表示本周的第幾天 ( 0 為周日)。
%x 不含時間的日期表示法。
%X 不含日期的時間表示法。 Eg: 15:26:30
%y 二位數字表示年份 (范圍由 00 至 99)。
%Y 完整的年份數字表示,即四位數。 Eg:2008
%Z(%z) 時區或名稱縮寫。Eg:中國標准時間
%% % 字元。
//方案二 優點:能精確到毫秒級;缺點:使用了windows API
#include <windows.h>
#include <stdio.h>
int main( void )
{
SYSTEMTIME sys;
GetLocalTime( &sys );
printf( "%4d/%02d/%02d %02d:%02d:%02d.%03d 星期%1d\n",sys.wYear,sys.wMonth,sys.wDay,sys.wHour,sys.wMinute, sys.wSecond,sys.wMilliseconds,sys.wDayOfWeek);
return 0;
}
//方案三,優點:利用系統函數,還能修改系統時間
//此文件必須是c++文件
#include<stdlib.h>
#include<iostream>
using namespace std;
void main()
{
system("time");
}
//方案四,將當前時間折算為秒級,再通過相應的時間換算即可
//此文件必須是c++文件
#include<iostream>
#include<ctime>
using namespace std;
int main()
{
time_t now_time;
now_time = time(NULL);
cout<<now_time;
return 0;
}
❸ 開發JAVA程序如何獲取系統編譯時間
import java.util.Date;
import java.text.SimpleDateFormat;
public class NowString {
public static void main(String[] args) {
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//設置日期格式
System.out.println(df.format(new Date()));// new Date()為獲取當前系統時間
}
}
❹ java 如何獲取當前系統時間
/**
* 獲取當前系統時間
*
* @return 返回短時間字元串格式yyyy-MM-dd
*/
public static String getStringDateShort() {
Date currentTime = new Date();
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
String dateString = formatter.format(currentTime);
return dateString;
}
**
* 獲取當前系統時間
*
* @return返回短時間格式 yyyy-MM-dd
*/
public static Date getNowDateShort() {
Date currentTime = new Date();
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
String dateString = formatter.format(currentTime);
ParsePosition pos = new ParsePosition(8);
Date currentTime_2 = formatter.parse(dateString, pos);
return currentTime_2;
}