/*
* Copyright (c) 2009, 2010, 2011, 2012, 2013 tinygao
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package jdk.date;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.commons.lang.StringUtils;
/**
* @author tingao
* 2013-1-5
*/
public final class DateUtils {
private static Logger log = Logger.getLogger(DateUtils.class.getName());
private static ThreadLocal<DateFormat> timeMap = new ThreadLocal<DateFormat>();
private static final String dateFormat = "yyyy-MM-dd hh:mm:ss";
/**
* 将时间转换成一定格式的日期字符串
* 如果dateFromat为空,就按照默认的格式输出
* @param date 日期
* @param dateFormat 转成字符串日期格式
* @return dateFormt格式的日期
* 2013-1-5 上午11:04:03
*/
public static String formatDate(Date date, String dateFormat) {
if(StringUtils.isEmpty(dateFormat)) {
dateFormat = DateUtils.dateFormat;
}
String result = "";
DateFormat dt = instanceDateFormat(dateFormat);
result = dt.format(date);
return result;
}
/**
* @param str
* @param dateFormat
* @return
* 2013-1-5 上午11:06:26
*/
public static Date parse(String str, String dateFormat) {
if(StringUtils.isEmpty(dateFormat)) {
dateFormat = DateUtils.dateFormat;
}
try {
DateFormat dt = instanceDateFormat(dateFormat);
return dt.parse(str);
} catch (ParseException e) {
log.log(Level.SEVERE, "从字符串转时间出错,详情:"+e.toString());
}
return null;
}
public static Date parse(String str) {
return parse(str, null);
}
public static String formatDate(Date date) {
return formatDate(date, null);
}
private static DateFormat instanceDateFormat(String dateFormat) {
DateFormat dt = timeMap.get();
if(dt == null) {
dt = new SimpleDateFormat(dateFormat);
timeMap.set(dt);
}
return dt;
}
//test
/*static class ThreadA implements Runnable {
int i = 0;
@Override
public void run() {
while(i++ < 100) {
String date = formatDate(new Date(), "yyyy-MM-dd");
System.out.println("线程A"+i+":" + date);
}
}
}
static class ThreadB implements Runnable {
int i = 0;
@Override
public void run() {
while(i++ < 100) {
String date = formatDate(new Date(), "yyyy-MM-dd hh:mm");
System.out.println("线程B"+i+":" + date);
}
}
}
public static void main(String[] args) {
String str = formatDate(new Date());
System.out.println(str);
Date date = parse("2013-01-05 11:11:16");
System.out.println(date);
ThreadA threadA = new ThreadA();
Thread tha = new Thread(threadA);
tha.start();
ThreadB threadB = new ThreadB();
Thread thb = new Thread(threadB);
thb.start();
}*/
}
分享到:
相关推荐
基于Java语言编写的Spring Boot时间工具类方法封装,主要方法如下:根据日期获取该日期内24小时的整点时刻、获取当月的 天数、根据 年、月 获取对应的月份 的 天数、根据指定日期得获取对应月份的所有日期的每日结束...
java日期时间工具类超级全。其中包含:获得当前日期 yyyy-MM-dd HH:mm:ss;获取系统当前时间戳;获取当前日期 yy-MM-dd;得到两个时间差 格式yyyy-MM-dd HH:mm:ss;转化long值的日期为yyyy-MM-dd HH:mm:ss.SSS格式的...
封装好的时间工具类, 包括时间格式转换,获取今天周几,一月几天。
java 时间 datatime 工具类
Java对时间进行操作的工具类,包括各种时间格式转化的方法以及一些时间比较登方法。
JAVA时间工具类(计算法定工作日工时):计算某个日期加上几个工作日后的一个工作日期(除周末和法定节假日),计算某个日期后一天的工作日期(除周末和法定节假日),计算两个日期的真实工作日(除周末和法定节假日),...
各种日期格式,时间戳,时间计算的相互转换。直接引入工程中,可使用
本人在项目开发中遇到的关于时间的操作,各位可以直接下载做工具类用
NULL 博文链接:https://fanshuyao.iteye.com/blog/2355386
20230621时间工具类
Java 时间工具类详解,主要包含了对时间操作的一些工具方法!将日期对象按照某种格式进行转换,将传入的日期对象按照yyyy-MM-dd格式转换成字符串返回,将插入的字符串按格式转换成对应的日期对象
没事写了一个关于java时间处理的工具类,很简单
日期时间工具类,样式美化,数据直接友好弹窗及转换处理
java时间工具类按照年月周维度在时间区间内输出时间点
SpringBoot中常用的时间工具类使用
基本方法较全 自带测试方式 不需要节假日等方法可以删除Lunar.java以及DateUtils的相关方法
java时间操作工具类,已封装好,包含获取某一时间的年,月,日,星期。以及对某一时间进行年,月,日,周的加减。还有获取某一时间年,月,日,周的起始和结束时间等等