取当前时间:
Calendar cal = new GregorianCalendar();
// Get the components of the time
int hour12 = cal.get(Calendar.HOUR); // 0..11
int hour24 = cal.get(Calendar.HOUR_OF_DAY); // 0..23
int min = cal.get(Calendar.MINUTE); // 0..59
int sec = cal.get(Calendar.SECOND); // 0..59
int ms = cal.get(Calendar.MILLISECOND); // 0..999
int ampm = cal.get(Calendar.AM_PM); // 0=AM, 1=PM取系统日期:
Calendar cal = new GregorianCalendar();
// Get the components of the date
int era = cal.get(Calendar.ERA); // 0=BC, 1=AD
int year = cal.get(Calendar.YEAR); // 2002
int month = cal.get(Calendar.MONTH); // 0=Jan, 1=Feb, ...
int day = cal.get(Calendar.DAY_OF_MONTH); // 1...
int dayOfWeek = cal.get(Calendar.DAY_OF_WEEK);
Calendar cal = new GregorianCalendar();
// Get the components of the time
int hour12 = cal.get(Calendar.HOUR); // 0..11
int hour24 = cal.get(Calendar.HOUR_OF_DAY); // 0..23
int min = cal.get(Calendar.MINUTE); // 0..59
int sec = cal.get(Calendar.SECOND); // 0..59
int ms = cal.get(Calendar.MILLISECOND); // 0..999
int ampm = cal.get(Calendar.AM_PM); // 0=AM, 1=PM取系统日期:
Calendar cal = new GregorianCalendar();
// Get the components of the date
int era = cal.get(Calendar.ERA); // 0=BC, 1=AD
int year = cal.get(Calendar.YEAR); // 2002
int month = cal.get(Calendar.MONTH); // 0=Jan, 1=Feb, ...
int day = cal.get(Calendar.DAY_OF_MONTH); // 1...
int dayOfWeek = cal.get(Calendar.DAY_OF_WEEK);
解决方案 »
- 基于JAVA的XML开发中如何引用JAR包中的DTD做验证?
- 好奇怪jsp,这也错!
- 一个困拢很久的servlet问题
- 我用的displaytag为什么不行
- 简单问题:JSP中如何读取配置文件?
- 请高手帮忙,客户端调用WebServices时遇到的异常问题,快急死了,请高手耐心帮忙看看吧
- 2除以3怎么让它等于0.66?
- 100分,在线请求apach+tomcat和jsp得混合 问题,其实很简单,但我一下部置到
- 我想用jsp,但是客户端又需要用到Activex?可以吗?
- 好烦人的小问题(jdbc)
- 防止死连接问题~~~~
- JSP中有一表格,四列,行数要动态生成,我想取出表格中的所有数据,怎么取?
import java.util.*;
/**
* <p>Title: Time </p>
* <p>Description: </p>
* 此类主要用来取得本地系统的系统时间并用下面5种格式显示
* 1. YYMMDDHH 8位
* 2. YYMMDDHHmm 10位
* 3. YYMMDDHHmmss 12位
* 4. YYYYMMDDHHmmss 14位
* 5. YYMMDDHHmmssxxx 15位 (最后的xxx 是毫秒)
* <p>Copyright: Copyright (c) 2003</p>
* <p>Company: hoten </p>
* @author lqf
* @version 1.0
*/
public class CTime {
public static final int YYMMDDhhmmssxxx=15;
public static final int YYYYMMDDhhmmss=14;
public static final int YYMMDDhhmmss=12;
public static final int YYMMDDhhmm=10;
public static final int YYMMDDhh=8;
/**
* 取得本地系统的时间,时间格式由参数决定
* @param format 时间格式由常量决定
* @return String 具有format格式的字符串
*/
public synchronized static String getTime(int format){
StringBuffer cTime=new StringBuffer(10);
Calendar time=Calendar.getInstance();
int miltime=time.get(Calendar.MILLISECOND);
int second=time.get(Calendar.SECOND);
int minute=time.get(Calendar.MINUTE);
int hour=time.get(Calendar.HOUR_OF_DAY);
int day =time.get(Calendar.DAY_OF_MONTH);
int month=time.get(Calendar.MONTH)+1;
int year =time.get(Calendar.YEAR);
if(format!=14){
if(year>=2000) year=year-2000;
else year=year-1900;
}
if(format>=2){
if(format==14) cTime.append(year);
else cTime.append(getFormatTime(year,2));
}
if(format>=4)
cTime.append(getFormatTime(month,2));
if(format>=6)
cTime.append(getFormatTime(day,2));
if(format>=8)
cTime.append(getFormatTime(hour,2));
if(format>=10)
cTime.append(getFormatTime(minute,2));
if(format>=12)
cTime.append(getFormatTime(second,2));
if(format>=15)
cTime.append(getFormatTime(miltime,3));
return cTime.toString();
}
/**
* 产生任意位的字符串
* @param time 要转换格式的时间
* @param format 转换的格式
* @return String 转换的时间
*/
private synchronized static String getFormatTime(int time,int format){
StringBuffer numm=new StringBuffer();
int length=String.valueOf(time).length(); if(format<length) return null; for(int i=0 ;i<format-length ;i++){
numm.append("0");
}
numm.append(time);
return numm.toString().trim();
}
}
取服务器时间:
String thistime = java.text.DateFormat.getDateTimeInstance().format(new java.util.Date());
取得时间后的格式是:yyyy-mm-dd hh;mm;ss
取本地时间用javascript就可以作到:
var thistime = new Date()
取得时间后的格式是:week month day hh;mm;ss UTC+0800(时区) year