日期时间显示的多种格式
日期时间显示的多种格式
1、日期时间显示的java类: 文件名:Datetime.java
2、在JSP文件中显示不同的日期时间格式:JSP文件名:datetime.jsp
3、日期时间脚本显示:文件名:time.htm
package net.xiaoxiang.dateTime;// 日期时间显示的多种格式类:文件名:Datetime.java
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;/*** 日期时间显示的多种格式类 以不同方法实现日期时间的不同显示格式* @author 逍湘* @version 1.0* @time 2007年7月26日 上午10时23分51秒*/
public class Datetime
{/*** 以字符串格式显示日期时间(Thu Jul 26 10:23:51 CST 2007)* @return datetime*/public String getDatetime_String1 ( ){String datetime = new Date ( ).toString ( );return datetime;}/*** 以字符串格式显示日期时间(26 Jul 2007 02:23:51 GMT)* @return datetime*/public String getDatetime_String2 ( ){String datetime = new Date ( ).toGMTString ( );return datetime;}/*** 以系统格式显示日期时间(yy-MM-dd 上午HH:mm)* @return datetime*/public String getDatetime_System ( ){DateFormat dt = DateFormat.getInstance ( );String datetime = dt.format ( new Date ( ) ).toString ( );return datetime;}/*** 以中国格式显示日期时间(xxxx年xx月xx日 下午xx时xx分xx秒)* @return datetime*/public String getDatetime_China ( ){DateFormat datetime1 = DateFormat.getDateInstance ( DateFormat.LONG, Locale.CHINA );DateFormat datetime2 = DateFormat.getTimeInstance ( DateFormat.LONG, Locale.CHINA );String datetime = datetime1.format ( new Date ( ) ) + " "+ datetime2.format ( new Date ( ) );return datetime;}/*** 以常用格式显示日期时间(yyyy-MM-dd HH:mm:ss+MILLISECOND)* @return datetime*/public String getDatetime_Standard ( ){Calendar now = Calendar.getInstance ( );String datetime = now.get ( Calendar.YEAR ) + "-"+ ( now.get ( Calendar.MONTH ) + 1 ) + "-"+ now.get ( Calendar.DAY_OF_MONTH ) + " " + now.get ( Calendar.HOUR )+ ":" + now.get ( Calendar.MINUTE ) + ":" + now.get ( Calendar.SECOND )+ now.get ( Calendar.MILLISECOND );return datetime;}/*** 以常用格式显示日期时间(yyyy-MM-dd HH:mm:ss)* @return datetime*/public String getDatetime ( ){String datetime = new Date ( ).toLocaleString ( );return datetime;}/*** 以简单系统格式显示日期时间(yyyy-MM-dd)* @return*/public String getDatetime_SimpleDateFormat ( ){try{SimpleDateFormat f = new java.text.SimpleDateFormat ( "yyyy-MM-dd" );String sDate = f.format ( new java.util.Date ( ) );java.util.Date dt = f.parse ( sDate );java.sql.Date sqlDate = new java.sql.Date ( dt.getTime ( ) );String datetime = sqlDate.toString ( );return datetime;}catch ( Exception ee ){ee.printStackTrace ( );return null;}}/*** 测试 显示结果* @param args*/public static void main ( String[] args ){// TODO 自动生成方法存根System.out.println ( "以字符串格式显示日期时间: " + new Datetime ( ).getDatetime_String1 ( ) );System.out.println ( "以字符串格式显示日期时间: " + new Datetime ( ).getDatetime_String2 ( ) );System.out.println ( "以系统格式显示日期时间: " + new Datetime ( ).getDatetime_System ( ) );System.out.println ( "以中国格式显示日期时间: " + new Datetime ( ).getDatetime_China ( ) );System.out.println ( "以常用格式显示日期时间: " + new Datetime ( ).getDatetime_Standard ( ) );System.out.println ( "以常用格式显示日期时间: " + new Datetime ( ).getDatetime ( ) );System.out.println ( "以简单系统格式显示日期: "+ new Datetime ( ).getDatetime_SimpleDateFormat ( ) );}
}
<%@ page language="java" pageEncoding="GBK"%> <%@ page import="java.util.*"%> <%@ page import="java.text.*;"%>日期时间显示 日期时间显示
<%//************ 以字符串格式显示日期时间 **********************************************Date time = new Date ( );out.print ( "字符串格式:" + time );%>
字符串格式:<%=new Date ( ).toGMTString ( )%>
<%//************ 以系统格式显示日期时间 **********************************************DateFormat df1 = DateFormat.getInstance ( );out.print ( "系统格式:" + df1.format ( new Date ( ) ) );%>
系统格式:<%=df1.format ( new Date ( ) )%>
<%//********** 以中国格式显示日期时间 **********************************************DateFormat df2 = DateFormat.getDateInstance ( DateFormat.LONG, Locale.CHINA );DateFormat df3 = DateFormat.getTimeInstance ( DateFormat.LONG, Locale.CHINA );out.print ( "中国格式:" + df2.format ( new Date ( ) ) + " " );out.print ( df3.format ( new Date ( ) ) );%>
中国格式:<%=df2.format ( new Date ( ) ) + " "+ df3.format ( new Date ( ) )%>
<%//********** 以常用格式显示日期时间 **********************************************Calendar now = Calendar.getInstance ( );String nowtime = now.get ( Calendar.YEAR ) + "-"+ ( now.get ( Calendar.MONTH ) + 1 ) + "-"+ now.get ( Calendar.DAY_OF_MONTH ) + " " + now.get ( Calendar.HOUR )+ ":" + now.get ( Calendar.MINUTE ) + ":"+ now.get ( Calendar.SECOND ) + now.get ( Calendar.MILLISECOND );out.print ( "常用格式:" + nowtime );%>
常用格式:<%=new Date ( ).toLocaleString ( )%>
<%//********** 以简单系统格式显示日期时间 **********************************************String datetime = "";try{SimpleDateFormat f = new java.text.SimpleDateFormat ( "yyyy-MM-dd" );String sDate = f.format ( new java.util.Date ( ) );java.util.Date dt = f.parse ( sDate );java.sql.Date sqlDate = new java.sql.Date ( dt.getTime ( ) );datetime = sqlDate.toString ( );}catch ( Exception ee ){ee.printStackTrace ( );}out.print ( "简单系统格式:" + datetime );%>
简单系统格式:<%=datetime%>
<%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" errorPage="" %>日期时间脚本显示 日期时间脚本显示
本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!
