Java8中时间的字符串和Long时间戳互转1、取当前时间戳
Long millisecond = w().toEpochMilli(); // 精确到毫秒
Long second = w().getEpochSecond();// 精确到秒
2、将Long类型的时间戳转成字符串
/**
* 将Long类型的时间戳转换成String 类型的时间格式,时间格式为:yyyy-MM-dd HH:mm:ss
*/
public static String timeToString(Long time){
DateTimeFormatter ftf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
return ftf.format(LocalDateTime.ofInstant(Instant.ofEpochMilli(time),ZoneId.systemDefault()));
}
3、时间字符串转成Long类型的时间戳
/**
* 将字符串转⽇期成Long类型的时间戳,格式为:yyyy-MM-dd HH:mm:ss
*/
public static Long timeToLong(String time) {
DateTimeFormatter ftf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
LocalDateTime parse = LocalDateTime.parse("2019-11-28 08:52:50", ftf);
return LocalDateTime.from(parse).atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
}
4、时间转换Utils
/**
* 将Long类型的时间戳转换成String 类型的时间格式,时间格式为:yyyy-MM-dd HH:mm:ss
*/
public static String timeToString(Long time, String format){
// "yyyy-MM-dd HH:mm:ss"
DateTimeFormatter formatString = DateTimeFormatter.ofPattern(format);
return formatString.format(LocalDateTime.ofInstant(Instant.ofEpochMilli(time), ZoneId.systemDefault()));
}
/**
* 将字符串转⽇期成Long类型的时间戳,格式为:yyyy-MM-dd HH:mm:ss
*/
public static Long timeToLong(String time) {
DateTimeFormatter formatString = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
LocalDateTime parse = LocalDateTime.parse("2019-11-28 08:52:50", formatString);
return LocalDateTime.from(parse).atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
}
/**
* localDate 格式化
*/
unix时间戳转换日期格式
public static String localDateFormat(LocalDate localDate, String format){
// "yyyy-MM-dd HH:mm:ss"
String localDateStr = DateTranslate.timeToString(localDate2Second(localDate), format);
return localDateStr;
}
/**
* localDate 格式化
*/
public static String localDateTimeFormat(LocalDateTime localDateTime, String format){
/
/ "yyyy-MM-dd HH:mm:ss"
String localDateTimeStr = DateTranslate.timeToString(localDateTime2Second(localDateTime), format);
return localDateTimeStr;
}
/**
* 取本⽉第⼀天
*/
public static LocalDate firstDayOfThisMonth() {
LocalDate today = w();
return today.with(TemporalAdjusters.firstDayOfMonth());
}
/**
* 取本⽉第⼀天
*/
public static LocalDate firstDayOfLastMonth() {
LocalDate today = w();
today = today.minusMonths(1);
return today.with(TemporalAdjusters.firstDayOfMonth());
}
/**
* 取本⽉第N天
*/
public static LocalDate dayOfThisMonth(int n) {
LocalDate today = w();
return today.withDayOfMonth(n);
}
/**
* 取本⽉最后⼀天
*/
public static LocalDate lastDayOfThisMonth() {
LocalDate today = w();
return today.with(TemporalAdjusters.lastDayOfMonth());
}
/
**
* 获取本周⼀
*/
public static LocalDateTime firstDayOfWeek(Long date) {
// long转LocalDateTime
LocalDateTime localDateTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(date), ZoneId.systemDefault());
return localDateTime.with(DayOfWeek.MONDAY);
}
/**
* 获取上周⼀
*/
public static LocalDateTime firstDayOfLastWeek(Long date) {
// long转LocalDateTime
LocalDateTime localDateTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(date), ZoneId.systemDefault());
LocalDateTime localDateTime1 = localDateTime.plusDays(-7);
return localDateTime1.with(DayOfWeek.MONDAY);
}
/**
* 获取上周⼀
*/
public static LocalDateTime lastDay(Long date) {
// long转LocalDateTime
LocalDateTime localDateTime = LocalDateTime.ofInstant(Instant.ofEpochSecond(date), ZoneId.systemDefault()); LocalDateTime localDateTime1 = localDateTime.plusDays(-1);
return localDateTime1;
}
/**
* 取本⽉第⼀天的开始时间
*/
public static LocalDateTime startOfThisMonth() {
return LocalDateTime.of(firstDayOfThisMonth(), LocalTime.MIN);
}
/**
* 取本⽉最后⼀天的结束时间
*/
public static LocalDateTime endOfThisMonth() {
return LocalDateTime.of(lastDayOfThisMonth(), LocalTime.MAX);
}
/** LocalDate转时间戳 */
public static Long localDate2Second(LocalDate localDate) {
return LocalDateTime.of(localDate, LocalTime.MIN).toInstant(ZoneOffset.ofHours(8)).getEpochSecond();
}
/** LocalDate转时间戳 */
public static Long localDateTime2Second(LocalDateTime localDateTime) {
EpochSecond(ZoneOffset.of("+8"));
}
java 8 Localdate常⽤API
====================================正确答案==================================
getYear() int获取当前⽇期的年份
getMonth() Month 获取当前⽇期的⽉份对象
getMonthValue() int获取当前⽇期是第⼏⽉
getDayOfWeek() DayOfWeek 表⽰该对象表⽰的⽇期是星期⼏
getDayOfMonth() int表⽰该对象表⽰的⽇期是这个⽉第⼏天
getDayOfYear() int表⽰该对象表⽰的⽇期是今年第⼏天
withYear(int year) LocalDate 修改当前对象的年份
withMonth(int month) LocalDate 修改当前对象的⽉份
withDayOfMonth(int dayOfMonth) LocalDate 修改当前对象在当⽉的⽇期
isLeapYear() boolean是否是闰年
lengthOfMonth() int这个⽉有多少天
lengthOfYear() int该对象表⽰的年份有多少天(365或者366)
plusYears(long yearsToAdd) LocalDate 当前对象增加指定的年份数
plusMonths(long monthsToAdd) LocalDate 当前对象增加指定的⽉份数
plusWeeks(long weeksToAdd) LocalDate 当前对象增加指定的周数
plusDays(long daysToAdd) LocalDate 当前对象增加指定的天数
minusYears(long yearsToSubtract) LocalDate 当前对象减去指定的年数
minusMonths(long monthsToSubtract) LocalDate 当前对象减去注定的⽉数
minusWeeks(long weeksToSubtract) LocalDate 当前对象减去指定的周数
minusDays(long daysToSubtract) LocalDate 当前对象减去指定的天数
compareTo(ChronoLocalDate other) int⽐较当前对象和other对象在时间上的⼤⼩,返回值如果为正,则当前对象时间较晚,isBefore(ChronoLocalDate other) boolean⽐较当前对象⽇期是否在other对象⽇期之前
isAfter(ChronoLocalDate other) boolean⽐较当前对象⽇期是否在other对象⽇期之后
isEqual(ChronoLocalDate other) boolean⽐较两个⽇期对象是否相等
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论