原文链接:
http://blog.csdn.net/wyp164/article/details/1625980
import java.util.Date; import java.text.SimpleDateFormat; class dayTime { public static void main(String args[]) { Date nowTime=new Date(); System.out.println(nowTime); SimpleDateFormat time=new SimpleDateFormat("yyyy MM dd HH mm ss"); System.out.println(time.format(nowTime)); } }
转载地址 http://tb.blog.csdn.net/TrackBack.aspx?PostId=380904 链接已无效! 1、通过MessageFormat转化 String dateTime = MessageFormat.format("{0,date,yyyy-MM-dd-HH-mm:ss:ms}" , new Object[] { new java.sql.Date(System.currentTimeMillis()) }); 说明: yyyy-MM-dd-HH-mm:ss:ms 年yyyy 月MM 日dd 时(大写为24进制,小写为12进制) 分mm 秒ss 微妙ms 2、修改数据库时用 queryDataSet.setDate("dateField", new java.sql.Date(new java.util.Date().getTime())); queryDataSet.setDate("dateField", new java.sql.Date.valueOf("2005-05-03")); 3、通过SimpleDateFormat 转化 SimpleDateFormat dateFm = new SimpleDateFormat("yyyy-MM-dd"); //格式化当前系统日期 String dateTime = dateFm.format(new java.util.Date()); 4、具体讨论 我们将讨论下面的类: 具体类可以被实例化, 但是抽象类却不能. 你首先必须实现抽象类的一个具体子类. Date 类从Java 开发包(JDK) 1.0 就开始进化, 当时它只包含了几个取得或者设置一个日期数据的各个部分的方法, 比如说月, 日, 和年. 这些方法现在遭到了批评并且已经被转移到了Calendar类里去了, 我们将在本文中进一步讨论它. 这种改进旨在更好的处理日期数据的国际化格式. 就象在JDK 1.1中一样, Date 类实际上只是一个包裹类, 它包含的是一个长整型数据, 表示的是从GMT(格林尼治标准时间)1970年, 1 月 1日00:00:00这一刻之前或者是之后经历的毫秒数. 一、创建一个日期对象 让我们看一个使用系统的当前日期和时间创建一个日期对象并返回一个长整数的简单例子. 这个时间通常被称为Java 虚拟机(JVM)主机环境的系统时间. import java.util.Date; public class DateExample1 { public static void main(String[] args) { // Get the system date/time Date date = new Date(); System.out.println(date.getTime()); } } 在星期六, 2001年9月29日, 下午大约是6:50的样子, 上面的例子在系统输出设备上显示的结果是 1001803809710. 在这个例子中,值得注意的是我们使用了Date 构造函数创建一个日期对象, 这个构造函数没有接受任何参数. 而这个构造函数在内部使用了System.currentTimeMillis() 方法来从系统获取日期. 那么, 现在我们已经知道了如何获取从1970年1月1日开始经历的毫秒数了. 我们如何才能以一种用户明白的格式来显示这个日期呢? 在这里类java.text.SimpleDateFormat 和它的抽象基类 java.text.DateFormat 就派得上用场了. 二、日期数据的定制格式 假如我们希望定制日期数据的格式, 比方星期六-9月-29日-2001年. 下面的例子展示了如何完成这个工作: import java.text.SimpleDateFormat; import java.util.Date; public class DateExample2 { public static void main(String[] args) { SimpleDateFormat bartDateFormat = new SimpleDateFormat("EEEE-MMMM-dd-yyyy"); Date date = new Date(); System.out.println(bartDateFormat.format(date)); } } 只要通过向SimpleDateFormat 的构造函数传递格式字符串"EEE-MMMM-dd-yyyy", 我们就能够指明自己想要的格式. 你应该可以看见, 格式字符串中的ASCII 字符告诉格式化函数下面显示日期数据的哪一个部分. EEEE是星期, MMMM是月, dd是日, yyyy是年. 字符的个数决定了日期是如何格式化的.传递"EE-MM-dd-yy"会显示 Sat-09-29-01. 请察看Sun 公司的Web 站点获取日期格式化选项的完整的指示.
import java.text.SimpleDateFormat; import java.util.Date; public class DateExample3 { public static void main(String[] args) { // Create a date formatter that can parse dates of // the form MM-dd-yyyy. SimpleDateFormat bartDateFormat = new SimpleDateFormat("MM-dd-yyyy"); // Create a string containing a text date to be parsed. String dateStringToParse = "9-29-2001"; try { // Parse the text version of the date. // We have to perform the parse method in a // try-catch construct in case dateStringToParse // does not contain a date in the format we are expecting. Date date = bartDateFormat.parse(dateStringToParse); // Now send the parsed date as a long value // to the system output. System.out.println(date.getTime()); } catch (Exception ex) { System.out.println(ex.getMessage()); } } } 四、使用标准的日期格式化过程 既然我们已经可以生成和解析定制的日期格式了, 让我们来看一看如何使用内建的格式化过程. 方法 DateFormat.getDateTimeInstance() 让我们得以用几种不同的方法获得标准的日期格式化过程. 在下面的例子中, 我们获取了四个内建的日期格式化过程. 它们包括一个短的, 中等的, 长的, 和完整的日期格式. import java.text.DateFormat; import java.util.Date; public class DateExample4 { public static void main(String[] args) { Date date = new Date(); DateFormat shortDateFormat = DateFormat.getDateTimeInstance( DateFormat.SHORT, DateFormat.SHORT); DateFormat mediumDateFormat = DateFormat.getDateTimeInstance( DateFormat.MEDIUM, DateFormat.MEDIUM); DateFormat longDateFormat = DateFormat.getDateTimeInstance( DateFormat.LONG, DateFormat.LONG); DateFormat fullDateFormat = DateFormat.getDateTimeInstance( DateFormat.FULL, DateFormat.FULL); System.out.println(shortDateFormat.format(date)); System.out.println(mediumDateFormat.format(date)); System.out.println(longDateFormat.format(date)); System.out.println(fullDateFormat.format(date)); } } 注意我们在对 getDateTimeInstance的每次调用中都传递了两个值. 第一个参数是日期风格, 而第二个参数是时间风格. 它们都是基本数据类型int(整型). 考虑到可读性, 我们使用了DateFormat 类提供的常量: SHORT, MEDIUM, LONG, 和 FULL. 要知道获取时间和日期格式化过程的更多的方法和选项, 请看Sun 公司Web 站点上的解释. 运行我们的例子程序的时候, 它将向标准输出设备输出下面的内容: 五、Calendar 类 我们现在已经能够格式化并创建一个日期对象了, 但是我们如何才能设置和获取日期数据的特定部分呢, 比如说小时, 日, 或者分钟? 我们又如何在日期的这些部分加上或者减去值呢? 答案是使用Calendar 类. 就如我们前面提到的那样, Calendar 类中的方法替代了Date 类中被人唾骂的方法. 假设你想要设置, 获取, 和操纵一个日期对象的各个部分, 比方一个月的一天或者是一个星期的一天. 为了演示这个过程, 我们将使用具体的子类 java.util.GregorianCalendar. 考虑下面的例子, 它计算得到下面的第十个星期五是13号. import java.util.GregorianCalendar; import java.util.Date; import java.text.DateFormat; public class DateExample5 { public static void main(String[] args) { DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.FULL); // Create our Gregorian Calendar. GregorianCalendar cal = new GregorianCalendar(); // Set the date and time of our calendar // to the system&s date and time cal.setTime(new Date()); System.out.println("System Date: " + dateFormat.format(cal.getTime())); // Set the day of week to FRIDAY cal.set(GregorianCalendar.DAY_OF_WEEK, GregorianCalendar.FRIDAY); System.out.println("After Setting Day of Week to Friday: " + dateFormat.format(cal.getTime())); int friday13Counter = 0; while (friday13Counter <= 10) { // Go to the next Friday by adding 7 days. cal.add(GregorianCalendar.DAY_OF_MONTH, 7); // If the day of month is 13 we have // another Friday the 13th. if (cal.get(GregorianCalendar.DAY_OF_MONTH) == 13) { friday13Counter++; System.out.println(dateFormat.format(cal.getTime())); } } } } 在这个例子中我们作了有趣的函数调用: 和: set 方法能够让我们通过简单的设置星期中的哪一天这个域来将我们的时间调整为星期五. 注意到这里我们使用了常量 DAY_OF_WEEK 和 FRIDAY来增强代码的可读性. add 方法让我们能够在日期上加上数值. 润年的所有复杂的计算都由这个方法自动处理. 我们这个例子的输出结果是: 六、时间掌握在你的手里 有了这些Date 和Calendar 类的例子, 你应该能够使用 java.util.Date, java.text.SimpleDateFormat, 和 java.util.GregorianCalendar 创建许多方法了. 5、具体讨论2 首先看一下Date类,Date中只提供了几个获得本地时间的方法,具体用法见下: import java.util.Date; public class DateFormatTest { public DateFormatTest() { } public static void main(String[] args) { Date dt=new Date(); String localeString=dt.toLocaleString(); String gmtString=dt.toGMTString(); System.out.println(localeString); System.out.println(gmtString); System.out.println(dt.getTime()); System.out.println(System.currentTimeMillis()); } } 输出结果为: 2004-12-31 9:34:04 import java.text.*; import java.util.Locale; import java.util.Date; public class DateFormatTest { public DateFormatTest() { } public static void main(String[] args) { DateFormat df=DateFormat.getDateInstance(); SimpleDateFormat defaultSDF=new SimpleDateFormat(); SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd"); System.out.println(new Date()); System.out.println(df.format(new Date())); System.out.println(defaultSDF.format(new Date())); System.out.println(sdf.format(new Date())); }} 输出结果是: Fri Dec 31 09:48:19 CST 2004 SimpleDateFormat的构造函数中String中字符的含义列如下表: import java.text.*; import java.util.Locale; import java.util.Date; public class DateFormatTest { public DateFormatTest() { } public static void main(String[] args) { String dateToparse="30-12-2004"; SimpleDateFormat sdf=new SimpleDateFormat("dd-MM-yyyy") Date dt=new Date() try{ dt=sdf.parse(dateToparse) System.out.print(dt) }catch(Exception e){ e.printStackTrace(); }}} |