java创建Date日期时间笔记
(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日期时间笔记的更多相关文章
- Java Date 日期 时间 相关方法
DateTools.java import java.text.SimpleDateFormat; import java.util.Date; /** * 日期操作类 */ public class ...
- 使用Java编写一个日期时间封装类
package base; import java.util.GregorianCalendar; import java.util.StringTokenizer; import java.util ...
- Java 8 新日期时间 API
Java 8 新日期时间 API 1. LocalDate.LocalTime.LocalDateTime LocalDate.LocalTime.LocalDateTime 实例是不可变的对象,分别 ...
- 在Jquery里格式化Date日期时间数据
在Jquery里格式化Date日期时间数据: $(function(){ //当前时间格式化yyyy-MM-dd HH:mm:ss alert(timeStamp2String(new Date(). ...
- java android中日期时间 问题总结
Date 类型: Date date = new Date(); // 代表获取当前系统日期和时间 System.out.println(date); 使用类的方法设置时间和日期:(通过该方法初始 ...
- java之Date(日期)、Date格式化、Calendar(日历)
参考http://how2j.cn/k/date/date-date/346.html Date(日期) Date类 注意:是java.util.Date; 而非 java.sql.Date,此类是给 ...
- java.util.Date转java.sql.Date丢失时间问题
java.sql.Date 只存储日期数据不存储时间数据// 会丢失时间数据preparedStatement.setDate(1, new java.sql.Date(date.getTime()) ...
- java里的日期时间
为了更好理解java的日期时间类,在这里我们先介绍一下关于历法.标准时间的一些概念. 历法有很多种,我们大中华上下五千年,自然也有自己的历法,生活中我们通常把自己传统的历法叫做农历,也有人叫它阴历或夏 ...
- java 的Date 日期相关操作
String 与 Date互转(1)基于SimpleDateFormat实现: package com.bky.df; import java.text.ParseException; import ...
随机推荐
- 资源下载南方cass视频教程,包括文档,数据,很全的
废话就不多说了,开始... 北方cass视频教程,包括文档,数据,很全的 视频下载地址:http://www.400gb.com/file/23459263 GIS网盘进入下载:http://laoh ...
- poj3250 Bad Hair Day
Description Some of Farmer John's N cows (1 ≤ N ≤ 80,000) are having a bad hair day! Since each cow ...
- Codeforces Round #146 (Div. 1) B. Let's Play Osu! dp
B. Let's Play Osu! 题目连接: http://www.codeforces.com/contest/235/problem/B Description You're playing ...
- .Net 下采用GET/POST/SOAP方式动态调用WebService的简易灵活方法(C#) [轉]Redfox
一直以来,我都为动态调用WebService方法而烦恼.在.Net环境下,最常用的方法就是采用代理类来调用WebService,可以通过改变代理类的Url属性来实现动态调用,但当xmlns改变时就会出 ...
- [ES6] 13. Using the ES6 spread operator ...
The spread operator (...) allows you to "explode" an array into its individual elements. S ...
- RMI、RPC、SOAP通讯技术介绍及比对 - XML/SOAP
RMI.RPC.SOAP通信技术介绍及比对 1.RMI 使用java的程序员,对于RMI(RemoteMethod Invoke,远程方法调用)一定不陌生,在java中,为了在分布式应用开发时,能够方 ...
- linux下网络编程常见问题
网络程序异常退出无core文件产生 这种情况发生在一边连接端已经关闭,但是另外一边还在对连接句柄做send操作,这样做send操作的进程会收到SIGPIPE信号,默认行为是直接退出且不会产生core. ...
- codeblocks中添加-std=c99
早上用codeblocks编译一个c文件,出现这样一个编译错误: +'for'+loop+initial+declarations+are+only+allowed+in+C99+mode 原来cod ...
- [原创]SQL SERVER 2008R2安装
配置系统环境说明 操作系统:Windows 7 操作系统版本:旗舰版 SP1 操作系统位数:x64 注:其它系统配置也基本相似,只是可能菜单的名字或者所处位置不一样,具体的配置如有不同,请自行搜索 安 ...
- gamework的使用方法
翻译来源地址:https://github.com/Kadoba/gamework gamework是控制LOVE2D游戏进程流的一个项目. ↑ 这个是按原文译的, 当初乍看完全不懂, 接下来我来用图 ...