package cn.mrcode.foodiedev.common.util;
import java.text.*;
import java.util.*;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateUtils;
public class DateUtil {
/**
* Base ISO 8601 Date format yyyyMMdd i.e., 20021225 for the 25th day of December in the year 2002
*/
public static final String ISO_DATE_FORMAT = "yyyyMMdd";
/**
* Expanded ISO 8601 Date format yyyy-MM-dd i.e., 2002-12-25 for the 25th day of December in the year 2002
*/
public static final String ISO_EXPANDED_DATE_FORMAT = "yyyy-MM-dd";
/**
* yyyy-MM-dd hh:mm:ss
*/
public static String DATETIME_PATTERN = "yyyy-MM-dd HH:mm:ss";
public static String DATE_PATTERN = "yyyyMMddHHmmss";
/**
* 则个
*/
private static boolean LENIENT_DATE = false;
private static Random random = new Random();
private static final int ID_BYTES = 10;
public synchronized static String generateId() {
StringBuffer result = new StringBuffer();
result = result.append(System.currentTimeMillis());
for (int i = 0; i < ID_BYTES; i++) {
result = result.append(random.nextInt(10));
}
return result.toString();
}
protected static final float normalizedJulian(float JD) {
float f = Math.round(JD + 0.5f) - 0.5f;
return f;
}
/**
* Returns the Date from a julian. The Julian date will be converted to noon GMT,
* such that it matches the nearest half-integer (i.e., a julian date of 1.4 gets
* changed to 1.5, and 0.9 gets changed to 0.5.)
*
* @param JD the Julian date
* @return the Gregorian date
*/
public static final Date toDate(float JD) {
/* To convert a Julian Day Number to a Gregorian date, assume that it is for 0 hours, Greenwich time (so
* that it ends in 0.5). Do the following calculations, again dropping the fractional part of all
* multiplicatons and divisions. Note: This method will not give dates accurately on the
* Gregorian Proleptic Calendar, i.e., the calendar you get by extending the Gregorian
* calendar backwards to years earlier than 1582. using the Gregorian leap year
* rules. In particular, the method fails if Y<400. */
float Z = (normalizedJulian(JD)) + 0.5f;
float W = (int) ((Z - 1867216.25f) / 36524.25f);
float X = (int) (W / 4f);
float A = Z + 1 + W - X;
float B = A + 1524;
float C = (int) ((B - 122.1) / 365.25);
float D = (int) (365.25f * C);
float E = (int) ((B - D) / 30.6001);
float F = (int) (30.6001f * E);
int day = (int) (B - D - F);
int month = (int) (E - 1);
if (month > 12) {
month = month - 12;
}
int year = (int) (C - 4715); //(if Month is January or February) or C-4716 (otherwise)
if (month > 2) {
year--;
}
Calendar c = Calendar.getInstance();
c.set(Calendar.YEAR, year);
c.set(Calendar.MONTH, month - 1); // damn 0 offsets
c.set(Calendar.DATE, day);
return c.getTime();
}
/**
* Returns the days between two dates. Positive values indicate that
* the second date is after the first, and negative values indicate, well,
* the opposite. Relying on specific times is problematic.
*
* @param early the "first date"
* @param late the "second date"
* @return the days between the two dates
*/
public static final int daysBetween(Date early, Date late) {
Calendar c1 = Calendar.getInstance();
Calendar c2 = Calendar.getInstance();
c1.setTime(early);
c2.setTime(late);
return daysBetween(c1, c2);
}
/**
* Returns the days between two dates. Positive values indicate that
* the second date is after the first, and negative values indicate, well,
* the opposite.
*
* @param early
* @param late
* @return the days between two dates.
*/
public static final int daysBetween(Calendar early, Calendar late) {
return (int) (toJulian(late) - toJulian(early));
}
/**
* Return a Julian date based on the input parameter. This is
* based from calculations found at
* <a href="http://quasar.as.utexas.edu/BillInfo/JulianDatesG.html">Julian Day Calculations
* (Gregorian Calendar)</a>, provided by Bill Jeffrys.
*
* @param c a calendar instance
* @return the julian day number
*/
public static final float toJulian(Calendar c) {
int Y = c.get(Calendar.YEAR);
int M = c.get(Calendar.MONTH);
int D = c.get(Calendar.DATE);
int A = Y / 100;
int B = A / 4;
int C = 2 - A + B;
float E = (int) (365.25f * (Y + 4716));
float F = (int) (30.6001f * (M + 1));
float JD = C + D + E + F - 1524.5f;
return JD;
}
/**
* Return a Julian date based on the input parameter. This is
* based from calculations found at
* <a href="http://quasar.as.utexas.edu/BillInfo/JulianDatesG.html">Julian Day Calculations
* (Gregorian Calendar)</a>, provided by Bill Jeffrys.
*
* @param date
* @return the julian day number
*/
public static final float toJulian(Date date) {
Calendar c = Calendar.getInstance();
c.setTime(date);
return toJulian(c);
}
/**
* @param isoString
* @param fmt
* @param field Calendar.YEAR/Calendar.MONTH/Calendar.DATE
* @param amount
* @return
* @throws ParseException
*/
public static final String dateIncrease(String isoString, String fmt,
int field, int amount) {
try {
Calendar cal = GregorianCalendar.getInstance(TimeZone.getTimeZone(
"GMT"));
cal.setTime(stringToDate(isoString, fmt, true));
cal.add(field, amount);
return dateToString(cal.getTime(), fmt);
} catch (Exception ex) {
return null;
}
}
/**
* Time Field Rolling function.
* Rolls (up/down) a single unit of time on the given time field.
*
* @param isoString
* @param field the time field.
* @param up Indicates if rolling up or rolling down the field value.
* @param expanded use formating char's
* @throws ParseException if an unknown field value is given.
*/
public static final String roll(String isoString, String fmt, int field,
boolean up) throws ParseException {
Calendar cal = GregorianCalendar.getInstance(TimeZone.getTimeZone(
"GMT"));
cal.setTime(stringToDate(isoString, fmt));
cal.roll(field, up);
return dateToString(cal.getTime(), fmt);
}
/**
* Time Field Rolling function.
* Rolls (up/down) a single unit of time on the given time field.
*
* @param isoString
* @param field the time field.
* @param up Indicates if rolling up or rolling down the field value.
* @throws ParseException if an unknown field value is given.
*/
public static final String roll(String isoString, int field, boolean up) throws
ParseException {
return roll(isoString, DATETIME_PATTERN, field, up);
}
/**
* java.util.Date
*
* @param dateText
* @param format
* @param lenient
* @return
*/
public static Date stringToDate(String dateText, String format,
boolean lenient) {
if (dateText == null) {
return null;
}
DateFormat df = null;
try {
if (format == null) {
df = new SimpleDateFormat();
} else {
df = new SimpleDateFormat(format);
}
/
没有合适的资源?快使用搜索试试~ 我知道了~
JAVA 架构练习上手小项目
共839个文件
png:242个
java:198个
jpg:115个
需积分: 0 1 下载量 53 浏览量
2023-09-20
20:00:19
上传
评论
收藏 18.23MB ZIP 举报
温馨提示
JAVA 架构练习上手小项目
资源推荐
资源详情
资源评论
收起资源包目录
JAVA 架构练习上手小项目
(839个子文件)
tomcat_https.cer 558B
amazeui.flat.css 322KB
amazeui.flat.css 322KB
amazeui.css 321KB
amazeui.css 321KB
amazeui.flat.min.css 242KB
amazeui.flat.min.css 242KB
amazeui.min.css 241KB
amazeui.min.css 241KB
hmstyle.css 43KB
hmstyle.css 43KB
style.css 17KB
style.css 17KB
demo.css 13KB
demo.css 13KB
personal.css 10KB
personal.css 10KB
jsstyle.css 9KB
jsstyle.css 9KB
systyle.css 9KB
systyle.css 9KB
infstyle.css 8KB
infstyle.css 8KB
orstyle.css 7KB
orstyle.css 7KB
cartstyle.css 7KB
cartstyle.css 7KB
seastyle.css 7KB
seastyle.css 7KB
paysuccess.css 6KB
paysuccess.css 6KB
wxpay.css 6KB
wxpay.css 6KB
sortstyle.css 6KB
sortstyle.css 6KB
blstyle.css 5KB
blstyle.css 5KB
dlstyle.css 5KB
dlstyle.css 5KB
admin.css 5KB
admin.css 5KB
optstyle.css 4KB
optstyle.css 4KB
refstyle.css 4KB
refstyle.css 4KB
addstyle.css 4KB
addstyle.css 4KB
footstyle.css 4KB
footstyle.css 4KB
cpstyle.css 4KB
cpstyle.css 4KB
colstyle.css 3KB
colstyle.css 3KB
blogstyle.css 3KB
blogstyle.css 3KB
appstyle.css 2KB
appstyle.css 2KB
stepstyle.css 2KB
stepstyle.css 2KB
cmstyle.css 2KB
cmstyle.css 2KB
jquery.mloading.css 2KB
bostyle.css 2KB
bostyle.css 2KB
sustyle.css 1KB
sustyle.css 1KB
newstyle.css 1KB
newstyle.css 1KB
bilstyle.css 1KB
bilstyle.css 1KB
lee.css 892B
lee.css 892B
lostyle.css 864B
lostyle.css 864B
zpageNav.css 849B
zpageNav.css 849B
item.css 444B
item.css 444B
skin.css 39B
skin.css 39B
app.css 23B
app.css 23B
fontawesome-webfont.eot 69KB
fontawesome-webfont.eot 69KB
loading.gif 307KB
loading.gif 307KB
loading.gif 6KB
loading.gif 6KB
loading.gif 6KB
loading.gif 6KB
566fda5cN4b8a1675.gif 4KB
566fda5cN4b8a1675.gif 4KB
checkbox.gif 2KB
checkbox.gif 2KB
sys_item_selected.gif 70B
sys_item_selected.gif 70B
.gitignore 273B
.gitignore 22B
.gitignore 11B
.gitignore 11B
共 839 条
- 1
- 2
- 3
- 4
- 5
- 6
- 9
资源评论
学习资源网
- 粉丝: 938
- 资源: 1587
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 206693250008_R01C02_Grn.idat
- 瞳孔跟踪-基于OpenCV+网络摄像头的瞳孔跟踪算法实现-附项目源码+流程教程-优质项目分享.zip
- ModStartCMS v8.4.0 框架稳定性持续迭代,修复部分已知问题
- bleder 教室学校学生教育室办公室考试
- 人脸检测-使用OpenCV实现的动漫+漫画人脸检测算法-附项目源码-优质项目实战.zip
- 道路贴图,材质材料免费
- 人脸检测-基于OpenCV+Node.js+WebSockets实现的实时人脸检测应用-附项目源码-优质项目实战.zip
- 一些常见的MySQL死锁案例-mysql-deadlocks-master(源代码+案例+图解说明)
- UE4动画烘焙器-ue4.27
- 新建文件夹.zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功