java中时间格式的转换
- 格式:docx
- 大小:15.14 KB
- 文档页数:4
关于java实体类时间类型的格式化调整问题关于java bean在后台\转化为json交给前台时间类型格式调整的⽅法:1. ⾸先要引⼊fastjson依赖。
2. 在实体类上使⽤注解: @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss",timezone = "GMT+8"),这⾥是精确到秒,如果只精确到天,就把上⾯注解的HH:mm:ss去掉。
package com.xjzt.waimai.pojo.weChatUser;import com.fasterxml.jackson.annotation.JsonFormat;import lombok.AllArgsConstructor;import lombok.Data;import lombok.NoArgsConstructor;import org.apache.ibatis.type.Alias;import java.io.Serializable;import java.util.Date;@Data@AllArgsConstructor@NoArgsConstructor@Alias("weCharUser")public class WeCharUser implements Serializable {private String openId;private String nickName;private String gender;private String province;private String city;@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss",timezone = "GMT+8")private Date createTime;@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss",timezone = "GMT+8")private Date lastVisitTime;}。
java时分的正则表达式Java时分的正则表达式正则表达式是一种描述字符串模式的语法,可以用来匹配、查找和替换文本中的字符。
在Java中,可以使用正则表达式对字符串进行各种操作,如验证邮箱格式、提取手机号码、判断字符串是否为数字等。
本文将介绍Java中常用的时分正则表达式,并解释其用法和示例。
一、匹配时间格式的正则表达式1. 匹配24小时制的时间格式:HH:mm:ss正则表达式:^[0-2][0-9]:[0-5][0-9]:[0-5][0-9]$示例:12:34:562. 匹配12小时制的时间格式:hh:mm:ss am/pm正则表达式:^(0[1-9]|1[0-2]):[0-5][0-9]:[0-5][0-9] (am|pm)$示例:09:45:30 am3. 匹配小时和分钟的时间格式:HH:mm正则表达式:^[0-2][0-9]:[0-5][0-9]$示例:20:154. 匹配12小时制的小时和分钟的时间格式:hh:mm am/pm正则表达式:^(0[1-9]|1[0-2]):[0-5][0-9] (am|pm)$示例:03:30 pm二、使用正则表达式的示例代码1. 验证时间格式是否正确:```javapublic boolean isValidTimeFormat(String time) {String regex = "^[0-2][0-9]:[0-5][0-9]:[0-5][0-9]$"; return time.matches(regex);}```2. 提取时间字符串中的小时和分钟:```javapublic String extractHourAndMinute(String time) { String regex = "^(\\d{2}):(\\d{2}):(\\d{2})$";Pattern pattern = pile(regex);Matcher matcher = pattern.matcher(time);if (matcher.find()) {String hour = matcher.group(1);String minute = matcher.group(2);return hour + ":" + minute;}return null;}```三、注意事项和常见问题1. 在使用正则表达式时,需要使用Java的转义字符,如\需要写成\\。
date类型转年月日格式如何将日期类型转换为年月日格式[导语]日期类型是编程中常见的一种数据类型,它表示一个特定的日期和时间。
在某些情况下,我们需要将日期类型转换为特定的格式,例如年月日格式。
本文将为您介绍如何一步步将日期类型转换为年月日格式。
[第一步:了解日期类型]在开始转换之前,我们需要先了解日期类型。
常见的日期类型有Date、DateTime和Timestamp。
在不同的编程语言中,日期类型的表示方式可能会有些差异。
在本文中,我们以Java为例进行讲解。
在Java中,日期类型通常使用java.util.Date类来表示。
[第二步:获取日期类型对象]要进行日期格式转换,首先我们需要获得一个日期类型的对象。
可以通过不同的方式来获取日期类型对象。
例如,可以使用系统的当前日期和时间来创建一个新的日期对象,或者从数据库中获取日期类型的数据。
下面是一个示例代码,演示如何获取一个日期类型对象:javaimport java.util.Date;public class DateConversionExample {public static void main(String[] args) {Date currentDate = new Date();System.out.println("当前日期和时间:" + currentDate);}}在这个示例中,我们使用了java.util.Date类的无参构造方法创建了一个新的日期对象currentDate,并通过System.out.println()方法将其打印出来。
通过运行这段代码,我们可以获取到当前的日期和时间。
[第三步:格式化日期类型]获取到日期类型对象后,我们可以使用SimpleDateFormat类来对日期进行格式化。
SimpleDateFormat是Java中常用的日期格式化类,它提供了多种格式化模式,可以根据需要选择不同的模式来转换日期格式。
java2个时间格式间转换(原创实用版)目录1.Java 中时间格式的表示2.时间格式的转换方法3.具体转换实例正文在 Java 编程语言中,时间格式的表示主要依赖于`java.time`包中的`LocalDateTime`和`ZonedDateTime`类。
这两个类分别用于表示不带时区的本地日期时间以及带时区的日期时间。
在实际编程中,我们常常需要对这些时间格式进行转换。
接下来,我们就来学习一下如何在 Java 中进行时间格式的转换。
一、Java 中时间格式的表示在 Java 中,时间格式主要通过`DateTimeFormatter`类进行表示。
`DateTimeFormatter`类提供了一系列的静态方法,用于创建不同格式的日期时间表示。
例如,我们可以使用`DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")`创建一个表示“年 - 月-日时:分:秒”的格式。
二、时间格式的转换方法在 Java 中,我们可以通过`DateTimeFormatter`类的`parse()`和`format()`方法进行时间格式的转换。
`parse()`方法用于将字符串格式的时间转换为`LocalDateTime`或`ZonedDateTime`对象,而`format()`方法则用于将`LocalDateTime`或`ZonedDateTime`对象转换为字符串格式的时间。
以下是一个简单的转换实例:```javaimport java.time.LocalDateTime;import java.time.ZoneId;import java.time.format.DateTimeFormatter;public class TimeFormatConversion {public static void main(String[] args) {// 创建一个表示“年 - 月-日时:分:秒”的DateTimeFormatterDateTimeFormatter formatter =DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");// 定义一个字符串格式的时间String dateTimeStr = "2022-01-01 12:30:45";// 使用 parse() 方法将字符串格式的时间转换为LocalDateTime 对象LocalDateTime localDateTime =LocalDateTime.parse(dateTimeStr, formatter);// 使用 format() 方法将 LocalDateTime 对象转换为字符串格式的时间String convertedDateTimeStr =localDateTime.format(formatter);// 输出转换后的时间System.out.println("转换后的时间:" + convertedDateTimeStr);}}```三、具体转换实例以下是一个具体的转换实例,演示如何将一个表示“年 - 月-日时:分:秒”的时间格式转换为“时分:秒.毫秒”格式:```javaimport java.time.LocalDateTime;import java.time.format.DateTimeFormatter;public class TimeFormatConversion {public static void main(String[] args) {// 创建一个表示“年 - 月-日时:分:秒”的DateTimeFormatterDateTimeFormatter formatter =DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");// 定义一个字符串格式的时间String dateTimeStr = "2022-01-01 12:30:45";// 使用 parse() 方法将字符串格式的时间转换为LocalDateTime 对象LocalDateTime localDateTime =LocalDateTime.parse(dateTimeStr, formatter);// 使用 format() 方法将 LocalDateTime 对象转换为字符串格式的时间String convertedDateTimeStr =localDateTime.format(DateTimeFormatter.ofPattern("HH:mm:ss.SSS" ));// 输出转换后的时间System.out.println("转换后的时间:" + convertedDateTimeStr);}}```运行上述代码,输出结果为:```转换后的时间:12:30:45.000```以上就是 Java 中时间格式间转换的相关知识。
java 国际标准时间字符串转换-回复如何将国际标准时间字符串转换为其他格式的时间表示方式。
这篇文章将从最基本的概念开始,逐步解释如何进行转换,并提供实际的示例来帮助读者理解和应用这些概念。
首先,我们来了解一下国际标准时间的概念。
国际标准时间(简称UTC)是一种使用的全球标准时间,用于统一不同地区的时间表示方式。
它基于原子钟的测量结果,具有高度的精确性和可靠性。
国际标准时间通常以24小时制的方式表示,格式为hh:mm:ss。
例如,13:30:00表示下午1点30分。
现在假设我们有一个国际标准时间字符串"09:45:30",我们想将其转换为其他格式的时间表示方式,例如12小时制的时间、时间戳等等。
下面是一步一步的解释:1. 将字符串解析为时间对象:首先,我们需要将国际标准时间字符串解析为程序能够理解的时间对象。
在Java中,可以使用SimpleDateFormat 类来完成这个任务。
以下是一个示例代码:javaString timeString = "09:45:30";SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");Date time = sdf.parse(timeString);在这个示例中,我们使用SimpleDateFormat来定义一个格式为"HH:mm:ss"的时间模板,然后将时间字符串解析为一个Date对象。
2. 转换为12小时制的时间表示方式:如果我们想将时间表示为12小时制的方式,我们需要使用另一个时间格式模板"hh:mm:ss a"。
以下是一个示例代码:javaSimpleDateFormat sdf12 = new SimpleDateFormat("hh:mm:ss a"); String time12 = sdf12.format(time);System.out.println(time12);在这个示例中,我们创建了另一个SimpleDateFormat对象,使用"hh:mm:ss a"作为时间格式模板。
java中timestamp 时间戳转换时间的方法(实用版4篇)目录(篇1)1.java中timestamp时间戳转换时间的方法2.时间戳的概念和作用3.java中常用的时间戳转换方法4.时间戳转换方法的优缺点5.总结正文(篇1)一、java中timestamp时间戳转换时间的方法在Java中,timestamp时间戳是一种常用的时间表示方式,可以记录时间的起始点。
时间戳通常用于记录事件发生的时间,例如日志记录、数据传输等。
在Java中,可以使用以下方法将timestamp转换为可读的时间格式:1.DateFormat类:DateFormat类可以将timestamp转换为可读的时间格式。
例如,可以使用以下代码将timestamp转换为Date对象:Date date = new Date(timestamp);2.Calendar类:Calendar类可以获取当前时间的各个部分,例如年、月、日、时、分、秒等。
可以使用以下代码将timestamp转换为Calendar 对象:Calendar calendar = Calendar.getInstance();calendar.setTimeInMillis(timestamp);3.SimpleDateFormat类:SimpleDateFormat类可以根据指定的格式将timestamp转换为可读的时间格式。
例如,可以使用以下代码将timestamp转换为String类型的时间格式:String time = new SimpleDateFormat("yyyy-MM-ddHH:mm:ss").format(new Date(timestamp));二、时间戳的概念和作用时间戳是指计算机系统自动生成的一个序列号,用于记录时间的起始点。
在计算机系统中,时间戳通常用于记录事件发生的时间,例如日志记录、数据传输等。
时间戳可以用于比较两个时间点之间的差异,例如计算两个事件之间的时间间隔。
java 国际标准时间字符串转换在Java编程中,我们经常需要将日期和时间从一种格式转换为另一种格式。
特别是当我们需要与国际标准时间(UTC)进行交互时,我们经常需要将本地时间转换为UTC时间。
在Java中,可以使用java.time包中的类来进行这种转换。
一、背景介绍Java8引入了新的日期和时间API,即java.time包。
这个包提供了简单易用的API来处理日期和时间。
它包括LocalDate、LocalTime、LocalDateTime、ZonedDateTime等类,可以方便地处理各种日期和时间相关的操作。
二、问题阐述在Java应用程序中,我们经常需要将本地时间转换为国际标准时间(UTC)。
例如,当与远程服务器进行通信时,我们需要将本地时间转换为UTC时间以便正确处理时间戳。
此外,在处理跨时区的数据时,也需要进行这样的转换。
三、解决方案为了将本地时间转换为国际标准时间(UTC),我们可以使用java.time包中的ZonedDateTime类和DateTimeFormatter类。
ZonedDateTime类提供了当前的日期和时间以及所在的时区信息。
通过将它转换为没有时区信息的ZonedDateTime对象,我们就可以得到相应的UTC时间。
以下是一个示例代码:```javaimportjava.time.ZoneId;importjava.time.ZonedDateTime;importjava.time.format.DateTimeFormatter;publicclassMain{publicstaticvoidmain(String[]args){//获取当前本地时间ZoneIdlocalZone=ZoneId.of("Asia/Shanghai");//替换为你的本地时区ZonedDateTimelocalDateTime=ZonedDateTime.now(localZone);System.out.println("Localtime:"+localDateTime);//将本地时间转换为UTC时间ZoneIdutcZone=ZoneId.of("Z");//Z代表UTC时区ZonedDateTimeutcDateTime=localDateTime.withZoneSameInstan t(utcZone);System.out.println("UTCtime:"+utcDateTime);}}```在这个示例中,我们首先获取了当前本地时间,并将其存储在ZonedDateTime对象中。
java中各种时间格式的转化时间和日期中常用到的几个类:java.util.Date,java.util.Calendar,java.util.GregorainCalendar,java.text.DateFormat,java.text.SimpleDateFormatjava.util.Date :表示特定瞬间,精确到毫秒一天是24 * 60 * 60 = 86400 秒世界时(UT 或UTC) , 格林威治时间(GMT), 格林威治时(GMT)和世界时(UT)是相等的,格林威治时(GMT) 是标准的"民间"称呼, 世界时(UT) 是相同标准的科学称呼。
UTC 和UT 的区别是:UTC 是基于原子时钟的,UT 是基于天体观察的。
(中文版的jdk 真是好啊,跟科普教材是的,呵呵)常用的构造方法:Date();Date(long date)(long date) 表示从标准基准时间(称为"历元" epoch ,即1970.1.100:00:00 GMT)经历的毫秒数。
还有一些构造方法已经deprecated 了主要的方法:(介绍几个常用的,详细的查询jdk 文档)boolean after(Date when) 测试日期在此日期之后。
boolean before(Date when) 测试日期在此日期之前。
Object clone() 返回此对象的副本long getTime() 返回自1970.1.1 00:00:00 GMT 后,此对象表示的毫秒数void setTime(long time) 设置此Date 对象,以表示1970 年1 月1 日00:00:00 GMT 以后time 毫秒的时间点。
String toString() 将Date 对象转化成以下形式的String:dow mon dd hh:mm:ss zzz yyyy其中:dow 是一周中的某一天(Sun, Mon, Tue, Wed, Thu, Fri, Sat)。
Java8中时间的字符串和Long时间戳互转1、取当前时间戳Long millisecond = Instant.now().toEpochMilli(); // 精确到毫秒Long second = Instant.now().getEpochSecond();// 精确到秒2、将Long类型的时间戳转成字符串/*** 将Long类型的时间戳转换成String 类型的时间格式,时间格式为:yyyy-MM-dd HH:mm:ss*/public static String timeToString(Long time){Assert.notNull(time, "time is null");DateTimeFormatter ftf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");return ftf.format(LocalDateTime.ofInstant(Instant.ofEpochMilli(time),ZoneId.systemDefault()));}3、时间字符串转成Long类型的时间戳/*** 将字符串转⽇期成Long类型的时间戳,格式为:yyyy-MM-dd HH:mm:ss*/public static Long timeToLong(String time) {Assert.notNull(time, "time is null");DateTimeFormatter ftf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");LocalDateTime parse = LocalDateTime.parse("2019-11-28 08:52:50", ftf);return LocalDateTime.from(parse).atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();}4、时间转换Utils/*** 将Long类型的时间戳转换成String 类型的时间格式,时间格式为:yyyy-MM-dd HH:mm:ss*/public static String timeToString(Long time, String format){Assert.notNull(time, "time is null");// "yyyy-MM-dd HH:mm:ss"DateTimeFormatter formatString = DateTimeFormatter.ofPattern(format);return formatString.format(LocalDateTime.ofInstant(Instant.ofEpochMilli(time), ZoneId.systemDefault()));}/*** 将字符串转⽇期成Long类型的时间戳,格式为:yyyy-MM-dd HH:mm:ss*/public static Long timeToLong(String time) {Assert.notNull(time, "time is null");DateTimeFormatter formatString = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");LocalDateTime parse = LocalDateTime.parse("2019-11-28 08:52:50", formatString);return LocalDateTime.from(parse).atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();}/*** localDate 格式化*/public static String localDateFormat(LocalDate localDate, String format){Assert.notNull(localDate, "time is null");// "yyyy-MM-dd HH:mm:ss"String localDateStr = DateTranslate.timeToString(localDate2Second(localDate), format);return localDateStr;}/*** localDate 格式化*/public static String localDateTimeFormat(LocalDateTime localDateTime, String format){Assert.notNull(localDateTime, "time is null");// "yyyy-MM-dd HH:mm:ss"String localDateTimeStr = DateTranslate.timeToString(localDateTime2Second(localDateTime), format);return localDateTimeStr;}/*** 取本⽉第⼀天*/public static LocalDate firstDayOfThisMonth() {LocalDate today = LocalDate.now();return today.with(TemporalAdjusters.firstDayOfMonth());}/*** 取本⽉第⼀天*/public static LocalDate firstDayOfLastMonth() {LocalDate today = LocalDate.now();today = today.minusMonths(1);return today.with(TemporalAdjusters.firstDayOfMonth());}/*** 取本⽉第N天*/public static LocalDate dayOfThisMonth(int n) {LocalDate today = LocalDate.now();return today.withDayOfMonth(n);}/*** 取本⽉最后⼀天*/public static LocalDate lastDayOfThisMonth() {LocalDate today = LocalDate.now();return today.with(stDayOfMonth());}/*** 获取本周⼀*/public static LocalDateTime firstDayOfWeek(Long date) {// long转LocalDateTimeLocalDateTime localDateTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(date), ZoneId.systemDefault());return localDateTime.with(DayOfWeek.MONDAY);}/*** 获取上周⼀*/public static LocalDateTime firstDayOfLastWeek(Long date) {// long转LocalDateTimeLocalDateTime localDateTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(date), ZoneId.systemDefault());LocalDateTime localDateTime1 = localDateTime.plusDays(-7);return localDateTime1.with(DayOfWeek.MONDAY);}/*** 获取上周⼀*/public static LocalDateTime lastDay(Long date) {// long转LocalDateTimeLocalDateTime localDateTime = LocalDateTime.ofInstant(Instant.ofEpochSecond(date), ZoneId.systemDefault()); LocalDateTime localDateTime1 = localDateTime.plusDays(-1);return localDateTime1;}/*** 取本⽉第⼀天的开始时间*/public static LocalDateTime startOfThisMonth() {return LocalDateTime.of(firstDayOfThisMonth(), LocalTime.MIN);}/*** 取本⽉最后⼀天的结束时间*/public static LocalDateTime endOfThisMonth() {return LocalDateTime.of(lastDayOfThisMonth(), LocalTime.MAX);}/** LocalDate转时间戳 */public static Long localDate2Second(LocalDate localDate) {return LocalDateTime.of(localDate, LocalTime.MIN).toInstant(ZoneOffset.ofHours(8)).getEpochSecond();}/** LocalDate转时间戳 */public static Long localDateTime2Second(LocalDateTime localDateTime) {return localDateTime.toEpochSecond(ZoneOffset.of("+8"));}java 8 Localdate常⽤API====================================正确答案==================================getYear() int获取当前⽇期的年份getMonth() Month 获取当前⽇期的⽉份对象getMonthValue() int获取当前⽇期是第⼏⽉getDayOfWeek() DayOfWeek 表⽰该对象表⽰的⽇期是星期⼏getDayOfMonth() int表⽰该对象表⽰的⽇期是这个⽉第⼏天getDayOfYear() int表⽰该对象表⽰的⽇期是今年第⼏天withYear(int year) LocalDate 修改当前对象的年份withMonth(int month) LocalDate 修改当前对象的⽉份withDayOfMonth(int dayOfMonth) LocalDate 修改当前对象在当⽉的⽇期isLeapYear() boolean是否是闰年lengthOfMonth() int这个⽉有多少天lengthOfYear() int该对象表⽰的年份有多少天(365或者366)plusYears(long yearsToAdd) LocalDate 当前对象增加指定的年份数plusMonths(long monthsToAdd) LocalDate 当前对象增加指定的⽉份数plusWeeks(long weeksToAdd) LocalDate 当前对象增加指定的周数plusDays(long daysToAdd) LocalDate 当前对象增加指定的天数minusYears(long yearsToSubtract) LocalDate 当前对象减去指定的年数minusMonths(long monthsToSubtract) LocalDate 当前对象减去注定的⽉数minusWeeks(long weeksToSubtract) LocalDate 当前对象减去指定的周数minusDays(long daysToSubtract) LocalDate 当前对象减去指定的天数compareTo(ChronoLocalDate other) int⽐较当前对象和other对象在时间上的⼤⼩,返回值如果为正,则当前对象时间较晚,isBefore(ChronoLocalDate other) boolean⽐较当前对象⽇期是否在other对象⽇期之前isAfter(ChronoLocalDate other) boolean⽐较当前对象⽇期是否在other对象⽇期之后isEqual(ChronoLocalDate other) boolean⽐较两个⽇期对象是否相等。
1.将日期类型转换为格式的字符串类型java.util.Date中的日期Date Date date = new Date();SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");String time=sdf.format(date);2.将字符串类型的转换为指定格式的日期类型(java.util.Date)String time = "2009-7-29 14:28:12";DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");Date date = df.parse(time);这里需要抛出ParseException异常若将java.util.Date类型转换成java.sql.Date时,只需添加一行代码java.sql.Date sqlDate = new java.sql.Date(date.getTime());3.使用java.sql.Timestamp在java程序中rs.getDate()时获取日期的时、分、秒String str = "";String format="yyyy-MM-dd HH:mm:ss";try {//这里与数据库的连接已经省略,获得PreparedStatement的对象psmtResultSet rs = psmt.executeQuery();while(rs.next()){Timestamp ts = rs.getTimestamp("pdate");SimpleDateFormat sdf = new SimpleDateFormat(format);str = sdf.format(ts);System.out.println(str);}} catch (SQLException e) {e.printStackTrace(); } ......SimpleDateFormat的用法://SimpleDateFormat中的parse方法可以把String型的字符串转换成特定格式的date类型import java.text.*;import java.util.*;public class TestDate {public static void main(String[] args) {String dStr = "2001.12.12-08.23.21";Date d = null;SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd-HH.mm.ss");try {d = sdf.parse(dStr);} catch (ParseException pe) {System.out.println(pe.getMessage());}System.out.println(d);System.out.println(d.getTime());}}//下面的format方法可以将date型的数据转换成特定的String型字符串public class FormatDateTime {public static void main(String[] args) {SimpleDateFormat myFmt=new SimpleDateFormat("yyyy年MM月dd日HH时mm分ss秒");SimpleDateFormat myFmt1=new SimpleDateFormat("yy/MM/dd HH:mm");SimpleDateFormat myFmt2=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//等价于now.toLocaleString()SimpleDateFormat myFmt3=new SimpleDateFormat("yyyy年MM月dd日HH时mm分ss秒E ");SimpleDateFormat myFmt4=new SimpleDateFormat("一年中的第D 天一年中第w个星期一月中第W个星期在一天中k时z时区");Date now=new Date();System.out.println(myFmt.format(now));System.out.println(myFmt1.format(now));System.out.println(myFmt2.format(now));System.out.println(myFmt3.format(now));System.out.println(myFmt4.format(now));System.out.println(now.toGMTString());System.out.println(now.toLocaleString());System.out.println(now.toString());}}效果:2004年12月16日17时24分27秒04/12/16 17:242004-12-16 17:24:272004年12月16日17时24分27秒星期四一年中的第351 天一年中第51个星期一月中第3个星期在一天中17时CST时区16 Dec 2004 09:24:27 GMT2004-12-16 17:24:27Thu Dec 16 17:24:27 CST 2004下面是个JavaBean:public class FormatDateTime {public static String toLongDateString(Date dt){SimpleDateFormat myFmt=new SimpleDateFormat("yyyy年MM月dd日HH时mm分ss 秒E ");return myFmt.format(dt);}public static String toShortDateString(Date dt){SimpleDateFormat myFmt=new SimpleDateFormat("yy年MM月dd日HH时mm分");return myFmt.format(dt);}public static String toLongTimeString(Date dt){SimpleDateFormat myFmt=new SimpleDateFormat("HH mm ss SSSS");return myFmt.format(dt);}public static String toShortTimeString(Date dt){SimpleDateFormat myFmt=new SimpleDateFormat("yy/MM/dd HH:mm");return myFmt.format(dt);}public static void main(String[] args) {Date now=new Date();System.out.println(FormatDateTime.toLongDateString(now));System.out.println(FormatDateTime.toShortDateString(now));System.out.println(FormatDateTime.toLongTimeString(now));System.out.println(FormatDateTime.toShortTimeString(now));}}调用的main 测试结果:2004年12月16日17时38分26秒星期四04年12月16日17时38分17 38 26 096504/12/16 17:3824小时制时间显示:public class Datetime {public static void main(String args[]){java.util.Date current=new java.util.Date();java.text.SimpleDateFormat sdf=new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");String c=sdf.format(current);System.out.println(c);}}12小时制时间显示:public class Datetime {public static void main(String args[]){java.util.Date current=new java.util.Date();java.text.SimpleDateFormat sdf=new java.text.SimpleDateFormat("yyyy-MM-dd hh:mm:ss");String c=sdf.format(current);System.out.println(c);}}。