- 浏览: 1054001 次
- 性别:
- 来自: 上海
文章分类
- 全部博客 (1441)
- 软件思想&演讲 (9)
- 行业常识 (250)
- 时时疑问 (5)
- java/guava/python/php/ruby/R/scala/groovy (213)
- struct/spring/springmvc (37)
- mybatis/hibernate/JPA (10)
- mysql/oracle/sqlserver/db2/mongdb/redis/neo4j/GreenPlum/Teradata/hsqldb/Derby/sakila (268)
- js/jquery/jqueryUi/jqueryEaseyUI/extjs/angulrJs/react/es6/grunt/zepto/raphael (81)
- ZMQ/RabbitMQ/ActiveMQ/JMS/kafka (17)
- lucene/solr/nuth/elasticsearch/MG4J (167)
- html/css/ionic/nodejs/bootstrap (19)
- Linux/shell/centos (56)
- cvs/svn/git/sourceTree/gradle/ant/maven/mantis/docker/Kubernetes (26)
- sonatype nexus (1)
- tomcat/jetty/netty/jboss (9)
- 工具 (17)
- ETL/SPASS/MATLAB/RapidMiner/weka/kettle/DataX/Kylin (11)
- hadoop/spark/Hbase/Hive/pig/Zookeeper/HAWQ/cloudera/Impala/Oozie (190)
- ios/swift/android (9)
- 机器学习&算法&大数据 (18)
- Mesos是Apache下的开源分布式资源管理框架 (1)
- echarts/d3/highCharts/tableau (1)
- 行业技能图谱 (1)
- 大数据可视化 (2)
- tornado/ansible/twisted (2)
- Nagios/Cacti/Zabbix (0)
- eclipse/intellijIDEA/webstorm (5)
- cvs/svn/git/sourceTree/gradle/jira/bitbucket (4)
- jsp/jsf/flex/ZKoss (0)
- 测试技术 (2)
- splunk/flunm (2)
- 高并发/大数据量 (1)
- freemarker/vector/thymeleaf (1)
- docker/Kubernetes (2)
- dubbo/ESB/dubboX/wso2 (2)
最新评论
、Date and its subclasses:
java.util.Date
java.sql.Date
java.sql.Timestamp
2、The calendar and time zone classes:
java.util.Calendar
java.util.GregorianCalendar
java.util.TimeZone
java.util.SimpleTimeZone //(for use with the Gregorian calendar only)
3、The formatting and parsing classes:
java.text.DateFormat
java.text.SimpleDateFormat
java.text.DateFormatSymbols
这些处理起来是非常之烦的。如果用DATE4J处理就容易很多了。例子如下:
package hirondelle.date4j;
import java.util.Locale;
import java.util.TimeZone;
/** Examples of how to use date4j. */
public final class Examples {
public static void main(String... aArgs){
Examples examples = new Examples();
examples.currentDateTime();
examples.currentDateTimeInCairo();
examples.ageIfBornOnCertainDate();
examples.optionsExpiry();
examples.daysTillChristmas();
examples.whenIs90DaysFromToday();
examples.whenIs3Months5DaysFromToday();
examples.hoursDifferenceBetweenParisAndPerth();
examples.weeksSinceStart();
examples.timeTillMidnight();
examples.imitateISOFormat();
examples.firstDayOfThisWeek();
examples.jdkDatesSuctorial();
}
// PRIVATE
private static void log(Object aMsg){
System.out.println(String.valueOf(aMsg));
}
/** What is the current date-time in the JRE's default time zone? */
private void currentDateTime(){
DateTime now = DateTime.now(TimeZone.getDefault());
String result = now.format("YYYY-MM-DD hh:mm:ss");
log("Current date-time in default time zone : " + result);
}
/** What is the current date-time in Cairo (include weekday)? */
private void currentDateTimeInCairo(){
DateTime now = DateTime.now(TimeZone.getTimeZone("Africa/Cairo"));
String result = now.format("YYYY-MM-DD hh:mm:ss (WWWW)", Locale.getDefault());
log("Current date-time in Cairo : " + result);
}
/** What's the age of someone born May 16, 1995? */
private void ageIfBornOnCertainDate(){
DateTime today = DateTime.today(TimeZone.getDefault());
DateTime birthdate = DateTime.forDateOnly(1995, 5, 16);
int age = today.getYear() - birthdate.getYear();
if(today.getDayOfYear() < birthdate.getDayOfYear()){
age = age - 1;
}
log("Age of someone born May 16, 1995 is : " + age);
}
/** Stock options expire on the 3rd Friday of this month. What day of the month is that? */
private void optionsExpiry(){
DateTime today = DateTime.today(TimeZone.getDefault());
DateTime firstOfMonth = today.getStartOfMonth();
int result = 0;
if (firstOfMonth.getWeekDay() == 7){
result = 21;
}
else {
result = 21 - firstOfMonth.getWeekDay();
}
DateTime thirdFriday = DateTime.forDateOnly(firstOfMonth.getYear(), firstOfMonth.getMonth(), result);
log("The 3rd Friday of this month is : " + thirdFriday.format("YYYY-MM-DD"));
}
/** How many days till the next December 25? */
private void daysTillChristmas(){
DateTime today = DateTime.today(TimeZone.getDefault());
DateTime christmas = DateTime.forDateOnly(today.getYear(), 12, 25);
int result = 0;
if(today.isSameDayAs(christmas)){
// do nothing
}
else if (today.lt(christmas)){
result = today.numDaysFrom(christmas);
}
else if (today.gt(christmas)){
DateTime christmasNextYear = DateTime.forDateOnly(today.getYear() + 1, 12, 25);
result = today.numDaysFrom(christmasNextYear);
}
log("Number of days till Christmas : " + result);
}
/** What day is 90 days from today? */
private void whenIs90DaysFromToday(){
DateTime today = DateTime.today(TimeZone.getDefault());
log("90 days from today is : " + today.plusDays(90).format("YYYY-MM-DD"));
}
/** What day is 3 months and 5 days from today? */
private void whenIs3Months5DaysFromToday(){
DateTime today = DateTime.today(TimeZone.getDefault());
DateTime result = today.plus(0,3,5,0,0,0,0,DateTime.DayOverflow.FirstDay);
log("3 months and 5 days from today is : " + result.format("YYYY-MM-DD"));
}
/** Current number of hours difference between Paris, France and Perth, Australia. */
private void hoursDifferenceBetweenParisAndPerth(){
//this assumes the time diff is a whole number of hours; other styles are possible
DateTime paris = DateTime.now(TimeZone.getTimeZone("Europe/Paris"));
DateTime perth = DateTime.now(TimeZone.getTimeZone("Australia/Perth"));
int result = perth.getHour() - paris.getHour();
if( result < 0 ) {
result = result + 24;
}
log("Numbers of hours difference between Paris and Perth : " + result);
}
/** How many weeks is it since Sep 6, 2010? */
private void weeksSinceStart(){
DateTime today = DateTime.today(TimeZone.getDefault());
DateTime startOfProject = DateTime.forDateOnly(2010, 9, 6);
int result = today.getWeekIndex() - startOfProject.getWeekIndex();
log("The number of weeks since Sep 6, 2010 : " + result);
}
/** How much time till midnight? */
private void timeTillMidnight(){
DateTime now = DateTime.now(TimeZone.getDefault());
DateTime midnight = now.plusDays(1).getStartOfDay();
long result = now.numSecondsFrom(midnight);
log("This many seconds till midnight : " + result);
}
/** Format using ISO style. */
private void imitateISOFormat(){
DateTime now = DateTime.now(TimeZone.getDefault());
log("Output using an ISO format: " + now.format("YYYY-MM-DDThh:mm:ss"));
}
private void firstDayOfThisWeek(){
DateTime today = DateTime.today(TimeZone.getDefault());
DateTime firstDayThisWeek = today; //start value
int todaysWeekday = today.getWeekDay();
int SUNDAY = 1;
if(todaysWeekday > SUNDAY){
int numDaysFromSunday = todaysWeekday - SUNDAY;
firstDayThisWeek = today.minusDays(numDaysFromSunday);
}
log("The first day of this week is : " + firstDayThisWeek);
}
/** For how many years has the JDK date-time API been suctorial? */
private void jdkDatesSuctorial(){
DateTime today = DateTime.today(TimeZone.getDefault());
DateTime jdkFirstPublished = DateTime.forDateOnly(1996, 1, 23);
int result = today.getYear() - jdkFirstPublished.getYear();
log("The number of years the JDK date-time API has been suctorial : " + result);
}
}
java.util.Date
java.sql.Date
java.sql.Timestamp
2、The calendar and time zone classes:
java.util.Calendar
java.util.GregorianCalendar
java.util.TimeZone
java.util.SimpleTimeZone //(for use with the Gregorian calendar only)
3、The formatting and parsing classes:
java.text.DateFormat
java.text.SimpleDateFormat
java.text.DateFormatSymbols
这些处理起来是非常之烦的。如果用DATE4J处理就容易很多了。例子如下:
package hirondelle.date4j;
import java.util.Locale;
import java.util.TimeZone;
/** Examples of how to use date4j. */
public final class Examples {
public static void main(String... aArgs){
Examples examples = new Examples();
examples.currentDateTime();
examples.currentDateTimeInCairo();
examples.ageIfBornOnCertainDate();
examples.optionsExpiry();
examples.daysTillChristmas();
examples.whenIs90DaysFromToday();
examples.whenIs3Months5DaysFromToday();
examples.hoursDifferenceBetweenParisAndPerth();
examples.weeksSinceStart();
examples.timeTillMidnight();
examples.imitateISOFormat();
examples.firstDayOfThisWeek();
examples.jdkDatesSuctorial();
}
// PRIVATE
private static void log(Object aMsg){
System.out.println(String.valueOf(aMsg));
}
/** What is the current date-time in the JRE's default time zone? */
private void currentDateTime(){
DateTime now = DateTime.now(TimeZone.getDefault());
String result = now.format("YYYY-MM-DD hh:mm:ss");
log("Current date-time in default time zone : " + result);
}
/** What is the current date-time in Cairo (include weekday)? */
private void currentDateTimeInCairo(){
DateTime now = DateTime.now(TimeZone.getTimeZone("Africa/Cairo"));
String result = now.format("YYYY-MM-DD hh:mm:ss (WWWW)", Locale.getDefault());
log("Current date-time in Cairo : " + result);
}
/** What's the age of someone born May 16, 1995? */
private void ageIfBornOnCertainDate(){
DateTime today = DateTime.today(TimeZone.getDefault());
DateTime birthdate = DateTime.forDateOnly(1995, 5, 16);
int age = today.getYear() - birthdate.getYear();
if(today.getDayOfYear() < birthdate.getDayOfYear()){
age = age - 1;
}
log("Age of someone born May 16, 1995 is : " + age);
}
/** Stock options expire on the 3rd Friday of this month. What day of the month is that? */
private void optionsExpiry(){
DateTime today = DateTime.today(TimeZone.getDefault());
DateTime firstOfMonth = today.getStartOfMonth();
int result = 0;
if (firstOfMonth.getWeekDay() == 7){
result = 21;
}
else {
result = 21 - firstOfMonth.getWeekDay();
}
DateTime thirdFriday = DateTime.forDateOnly(firstOfMonth.getYear(), firstOfMonth.getMonth(), result);
log("The 3rd Friday of this month is : " + thirdFriday.format("YYYY-MM-DD"));
}
/** How many days till the next December 25? */
private void daysTillChristmas(){
DateTime today = DateTime.today(TimeZone.getDefault());
DateTime christmas = DateTime.forDateOnly(today.getYear(), 12, 25);
int result = 0;
if(today.isSameDayAs(christmas)){
// do nothing
}
else if (today.lt(christmas)){
result = today.numDaysFrom(christmas);
}
else if (today.gt(christmas)){
DateTime christmasNextYear = DateTime.forDateOnly(today.getYear() + 1, 12, 25);
result = today.numDaysFrom(christmasNextYear);
}
log("Number of days till Christmas : " + result);
}
/** What day is 90 days from today? */
private void whenIs90DaysFromToday(){
DateTime today = DateTime.today(TimeZone.getDefault());
log("90 days from today is : " + today.plusDays(90).format("YYYY-MM-DD"));
}
/** What day is 3 months and 5 days from today? */
private void whenIs3Months5DaysFromToday(){
DateTime today = DateTime.today(TimeZone.getDefault());
DateTime result = today.plus(0,3,5,0,0,0,0,DateTime.DayOverflow.FirstDay);
log("3 months and 5 days from today is : " + result.format("YYYY-MM-DD"));
}
/** Current number of hours difference between Paris, France and Perth, Australia. */
private void hoursDifferenceBetweenParisAndPerth(){
//this assumes the time diff is a whole number of hours; other styles are possible
DateTime paris = DateTime.now(TimeZone.getTimeZone("Europe/Paris"));
DateTime perth = DateTime.now(TimeZone.getTimeZone("Australia/Perth"));
int result = perth.getHour() - paris.getHour();
if( result < 0 ) {
result = result + 24;
}
log("Numbers of hours difference between Paris and Perth : " + result);
}
/** How many weeks is it since Sep 6, 2010? */
private void weeksSinceStart(){
DateTime today = DateTime.today(TimeZone.getDefault());
DateTime startOfProject = DateTime.forDateOnly(2010, 9, 6);
int result = today.getWeekIndex() - startOfProject.getWeekIndex();
log("The number of weeks since Sep 6, 2010 : " + result);
}
/** How much time till midnight? */
private void timeTillMidnight(){
DateTime now = DateTime.now(TimeZone.getDefault());
DateTime midnight = now.plusDays(1).getStartOfDay();
long result = now.numSecondsFrom(midnight);
log("This many seconds till midnight : " + result);
}
/** Format using ISO style. */
private void imitateISOFormat(){
DateTime now = DateTime.now(TimeZone.getDefault());
log("Output using an ISO format: " + now.format("YYYY-MM-DDThh:mm:ss"));
}
private void firstDayOfThisWeek(){
DateTime today = DateTime.today(TimeZone.getDefault());
DateTime firstDayThisWeek = today; //start value
int todaysWeekday = today.getWeekDay();
int SUNDAY = 1;
if(todaysWeekday > SUNDAY){
int numDaysFromSunday = todaysWeekday - SUNDAY;
firstDayThisWeek = today.minusDays(numDaysFromSunday);
}
log("The first day of this week is : " + firstDayThisWeek);
}
/** For how many years has the JDK date-time API been suctorial? */
private void jdkDatesSuctorial(){
DateTime today = DateTime.today(TimeZone.getDefault());
DateTime jdkFirstPublished = DateTime.forDateOnly(1996, 1, 23);
int result = today.getYear() - jdkFirstPublished.getYear();
log("The number of years the JDK date-time API has been suctorial : " + result);
}
}
发表评论
-
20180222积累
2018-02-22 09:34 4811. mybatis如何通过接口查找对应的mapper. ... -
20180208积累
2018-02-08 10:28 473临时表与永久表相似,但临时表存储在 tempdb 中,当不 ... -
行业应用
2018-01-30 16:30 491git clone的时候用上面那个IP地址,下面栏中的不能 ... -
SQLite 数据库
2018-01-29 22:57 756android: SQLite创建数据 ... -
java里面获取map的key和value的方法
2018-02-01 11:29 2164获取map的key和value的方法分为两种形式: ma ... -
Eclipse中Maven WEB工程tomcat项目添加调试以及项目发布细节记录
2018-02-23 21:11 732一、建立一个maven WEB项目 1、file-&g ... -
错误:HttpServlet was not found on the Java
2018-02-23 21:12 390我们在用Eclipse进行Java web ... -
使用 java8 实现List到Array的转换
2018-02-23 21:13 2994开发中需要调用第三方的库,有些 API 的入参要求是 do ... -
Java8 利用Lambda处理List集合
2018-01-11 09:58 5635Java 8新增的Lambda表达式,我们可以很方便地并行操 ... -
java中string与json互相转化
2018-01-11 09:40 1076在Java中socket传输数据时,数据类型往往比较难选择。 ... -
JSON 数据格式
2018-01-11 09:37 474JSON(JavaScript Object Notatio ... -
java怎么读取json格式的数据
2018-01-11 09:46 1060java可以使用JSONObject和JSONArray来操作 ... -
Java8-如何将List转变为逗号分隔的字符串
2018-01-10 10:13 2000Converting a List to a String ... -
eclipse maven 打war包的两种方式
2018-02-23 21:25 704第一种:利用pom.xml文件打包。 右键pom.xml ... -
Annotation(三)——Spring注解开发
2018-02-28 09:21 429Spring框架的核心功能IoC(Inversion o ... -
Spring自定义注解
2018-02-28 09:32 600java注解:附在代码中的一些元信息,用于在编译、运行时起 ... -
Java项目
2018-01-08 10:56 0这两种解决办法已经能完全解决问题,不过值得注意的一点是,我 ... -
解决Eclipse建立Maven项目后无法建立src/main/java资源文件夹的办法
2018-03-22 10:41 1134在Eclipse中建立好一个Maven项目后,如果Java ... -
Java @override报错的解决方法
2018-01-07 12:56 0有时候Java的Eclipse工程换一台电脑后编译总是@ove ... -
Java 8 配置Maven-javadoc-plugin
2018-01-07 09:07 1047在升级JDK至1.8之后,使用Maven-javadoc- ...
相关推荐
date4j是一个用于简化日期和时间操作的Java工具。可以替换java.util.Date。
date4j是一个用于简化日期和时间操作的Java工具。
Date4j的设计理念是提供一种更加直观的日期表示和操作方式。它引入了自己的日期类,如`DateTime`,这个类比Java内置的`Date`类更易于理解和使用。Date4j支持ISO 8601日期时间格式,这是国际上广泛采用的标准,使得...
`date4j`是一个轻量级的Java日期和时间库,它提供了对日期和时间操作的灵活处理。这个源代码压缩包包含`date4j`的源代码,这对于开发者来说非常有价值,因为可以深入理解其内部工作原理,进行自定义扩展或者调试。...
在Java中,最著名的工具类库是`java.util`包,它包含了大量实用类,如集合、日期时间、数学计算、线程管理等。此外,还有一些第三方库,如Apache Commons Lang、Guava等,提供了更丰富的功能。 1. **java.util包**...
Java工具类是编程实践中常用的辅助模块,它们提供了一系列静态方法,方便开发者进行常见的操作,比如字符串处理、日期时间操作、加密解密以及日志记录等。以下是对标题和描述中涉及知识点的详细解释: 1. **String...
下面我们将深入探讨几个常见的Java工具类库以及它们在实际开发中的应用。 1. **Java标准库中的工具类** - `java.util`: 这个包中包含了大量工具类,如`ArrayList`, `HashMap`, `LinkedList`等集合框架类,以及`...
同样来自Apache Commons Lang,`DateUtils`提供了日期和时间的操作方法,比如格式化日期、解析日期字符串、计算两个日期之间的差值等。相比Java内置的`java.util.Date`和`java.text.SimpleDateFormat`,它的使用...
Java工具概述 在Java开发中,很多复杂的算法和数据结构已经被实现并封装在标准库中,供开发者直接使用。这大大减轻了开发者的负担,使得他们能够专注于应用程序的核心逻辑。例如,数组排序这样的任务可以通过`...
5. **Java Date/Time API**: Java 8引入了全新的日期和时间API,位于`java.time`包下,如`LocalDate`、`LocalTime`、`LocalDateTime`、`ZonedDateTime`等类,以及`TemporalAdjusters`和`TemporalQueries`等工具类,...
5. **DateUtil.java**: 日期时间工具类,帮助开发者处理日期和时间,如格式化日期、计算两个日期之间的差值、获取当前时间等。通常会使用Java 8的`java.time`包或者旧版的`java.util.Date`和`java.text....
2. **日期时间工具类**:Java 8引入了`java.time`包,提供`LocalDate`, `LocalTime`, `LocalDateTime`等类,以及`java.time.format.DateTimeFormatter`进行日期时间格式化。而在旧版本中,`java.util.Date`和`java....
10. **JavaScript系统时间.txt**:这个文件可能是关于如何在JavaScript中获取和操作系统时间的说明或示例代码,JavaScript中的Date对象可以用来处理日期和时间相关操作。 以上这些工具和资源在Java开发,特别是J2EE...
概述Date4j 来自 Hirondelle Systems, " 和他的许可证 BSD ( ) DATE4J 是标准 JDK 日期类的简单替代品。... date4j 工具选择专注于数据库如何以简单的样式(没有时区/偏移量)存储日期和时间,而不是对民用计时的神秘
22. **System**: 提供系统相关的属性和操作,如获取当前时间、系统属性等。 23. **Currency**: 处理货币类型和汇率转换。 24. **UUID**: 生成全局唯一的128位标识符。 25. **CharsetEncoder 和 CharsetDecoder**:...
6. **Date** 和 **Calendar** 类:处理日期和时间的工具,但现代Java推荐使用**java.time**包中的类,如LocalDate, LocalDateTime和Duration等,它们更强大且易于使用。 7. **Random** 类:生成随机数,支持整数和...
3. **日期与时间**:处理日期和时间在Java中相对复杂,工具类可能封装了`java.util.Date`和`java.time`包中的API,提供易用的日期和时间转换、计算等功能。 4. **IO流操作**:Java的IO流操作通常涉及大量的读写和...