(1)public Date()

  创建的日期类对象的日期时间被设置成创建时刻相对应的日期时间。

  例:

Java代码

1.Date today=new Date();  //today被设置成创建时刻相对应的日期时间。 
  (2)public Date (long date)

  long 型的参数date可以通过调用Date类中的static方法parse(String s)来获得。

例:

Java代码

1.long l=Date.parse("Mon 6 Jan 1997 13:3:00"); 
2.Date day=new Date(l);  //day中时间为1997年 1月6号星期一,13:3:00。

(3)public Date(String s)

按字符串s产生一日期对象。s的格式与方法parse中字符串参数的模式相同。

例:

Java代码

1.Date day=new Date("Mon 6 Jan 1997 13:3:00");  //day 中时间为1997年1月6号星期一,13:3:00.

(4)按:年、月、日

Java代码 
1.public Date(int year,int month,int date)

(5)按:年、月、日、时、分

Java代码

1.public Date(int year,int month,int date,int hrs,int min)

(6)按:年、月、日、时、分、秒

Java代码

1.public Date(int year,int month,int date,int hrs,int min,int sec)

按给定的参数创建一日期对象。

year的值为:需设定的年份-1900。例如需设定的年份是1997则year的值应为97,即1997-1900的结果。所以Date中可设定的年份最小为1900;

  month的值域为0~11,0代表1月,11表代表12月;

  date的值域在1~31之间;

  hrs的值域在0~23之间。从午夜到次日凌晨1点间hrs=0,从中午到下午1点间hrs=12;

  min和sec的值域在0~59之间。

下面整理一些相关示例

创建一个日期对象:

代码如下 复制代码
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());
} }

日期数据的定制格式:

代码如下 复制代码
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));
} }

将文本数据解析成日期对象 :

代码如下 复制代码
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());
}
} }

时间比较:

代码如下 复制代码
import java.text.*;
import java.util.*;

public class TimeCompare{

public static void main(String[] args){
  boolean flag = isDateBefore('2004-09-09 12:12:12','2005-09-09 16:00:00');
  System.out.println(flag);
  flag = isDateBefore('2006-09-09 01:01:01','2005-09-09 16:00:00');
  System.out.println(flag);
  flag = isDateBefore('2005-09-09 01:01:01');
  System.out.println(flag);
 }

//判断时间date1是否在时间date2之前
 //时间格式 2005-4-21 16:16:34
 public static boolean isDateBefore(String date1,String date2){
  try{
   DateFormat df = DateFormat.getDateTimeInstance();
   return df.parse(date1).before(df.parse(date2));
  }catch(ParseException e){
   System.out.print('[SYS] ' + e.getMessage());
   return false;
  }
 }
 //判断当前时间是否在时间date2之前
 //时间格式 2005-4-21 16:16:34
 public static boolean isDateBefore(String date2){
  try{
   Date date1 = new Date();
   DateFormat df = DateFormat.getDateTimeInstance();
   return date1.before(df.parse(date2));
  }catch(ParseException e){
   System.out.print('[SYS] ' + e.getMessage());
   return false;
  }
 }

}

在java里怎么实现当前时间格式为yyyy-mm-dd hh:mm:ss和当前时间后30分钟:

代码如下 复制代码
import java.text.*;
import java.util.*;
public class L{
public static void main(String[] args){
Date date = new Date(System.currentTimeMillis());
DateFormat df = DateFormat.getDateTimeInstance(DateFormat.MEDIUM,DateFormat.MEDIUM,Locale.CHINA);
String dt = df.format(date);
System.out.println(dt);
date = new Date(System.currentTimeMillis() + 30 * 60 * 1000); //半小时以后的时间
dt = df.format(date);
System.out.println(dt);
}}

更多详细内容请查看:http://www.111cn.net/jsp/Java/56302.htm

java创建Date日期时间笔记的更多相关文章

  1. Java Date 日期 时间 相关方法

    DateTools.java import java.text.SimpleDateFormat; import java.util.Date; /** * 日期操作类 */ public class ...

  2. 使用Java编写一个日期时间封装类

    package base; import java.util.GregorianCalendar; import java.util.StringTokenizer; import java.util ...

  3. Java 8 新日期时间 API

    Java 8 新日期时间 API 1. LocalDate.LocalTime.LocalDateTime LocalDate.LocalTime.LocalDateTime 实例是不可变的对象,分别 ...

  4. 在Jquery里格式化Date日期时间数据

    在Jquery里格式化Date日期时间数据: $(function(){ //当前时间格式化yyyy-MM-dd HH:mm:ss alert(timeStamp2String(new Date(). ...

  5. java android中日期时间 问题总结

    Date 类型: Date date = new Date();   // 代表获取当前系统日期和时间 System.out.println(date); 使用类的方法设置时间和日期:(通过该方法初始 ...

  6. java之Date(日期)、Date格式化、Calendar(日历)

    参考http://how2j.cn/k/date/date-date/346.html Date(日期) Date类 注意:是java.util.Date; 而非 java.sql.Date,此类是给 ...

  7. java.util.Date转java.sql.Date丢失时间问题

    java.sql.Date 只存储日期数据不存储时间数据// 会丢失时间数据preparedStatement.setDate(1, new java.sql.Date(date.getTime()) ...

  8. java里的日期时间

    为了更好理解java的日期时间类,在这里我们先介绍一下关于历法.标准时间的一些概念. 历法有很多种,我们大中华上下五千年,自然也有自己的历法,生活中我们通常把自己传统的历法叫做农历,也有人叫它阴历或夏 ...

  9. java 的Date 日期相关操作

    String 与 Date互转(1)基于SimpleDateFormat实现: package com.bky.df; import java.text.ParseException; import ...

随机推荐

  1. .net MVC 碰到的问题

    1:问:回车会默认会触发页面从左边至右,从上到下索引位置第一的按钮事件.如何取消? 答:在不需要触发按钮事件的按钮中加一个属性:UseSubmitBehavior="false" ...

  2. 内​存​泄​露​调​试​之​ ​v​i​s​u​a​l​ ​l​e​a​k​ ​d​e​t​e​c​t​o​r​ ​工​具【转】

    本文参考此文:http://kangzj.net/visual-leak-detector-download/  另外一种检查内存泄露的工具:visual leak  detector  简称  vl ...

  3. CircleWaveProgressBar

    https://github.com/eltld/CircleWaveProgressBar

  4. Android---3种方式限制EditView输入字数(转载)

    方法一:利用TextWatcher editText.addTextChangedListener(new TextWatcher() { private CharSequence temp; pri ...

  5. 安卓开发之使用viewpager+fragment实现滚动tab页

    闲着.用viewpager+fragment实现了个滚动tab..轻拍,以后会陆续发先小东西出来..爱分享,才快乐.demo见附件.. package com.example.demo; import ...

  6. 黑客破译android开发代码真就那么简单?

    很多程序员辛辛苦苦开发出的android开发代码,很容易就被黑客翻译了. Google似乎也发现了这个问题,从SDK2.3开始我们可以看到在android-sdk-windows\tools\下面多了 ...

  7. Mongodb集群配置(sharding with replica set)

    转自:http://blog.csdn.net/zhangzhaokun/article/details/6269514 前言 最近在研习MongoDB集群,找到一个不错的例子,加了几句,按照自己的理 ...

  8. 使用 Team Foundation 版本控制命令

    使用 Team Foundation 版本控制命令 Visual Studio 2013   其他版本 Visual Studio 2010 Visual Studio 2008 Visual Stu ...

  9. Java IO和Java NIO在文件拷贝上的性能差异分析

    1.  在JAVA传统的IO系统中,读取磁盘文件数据的过程如下: 以FileInputStream类为例,该类有一个read(byte b[])方法,byte b[]是我们要存储读取到用户空间的缓冲区 ...

  10. <QtEndian> - Endian Conversion Functions

    The <QtEndian> header provides functions to convert between little and big endian representati ...