LocalDateTime基本使用

 1、时间戳相互转换,2、日期对象与字符串相互转换

package com.baye.common.core.util;


import org.springframework.util.StringUtils;

import javax.validation.constraints.NotNull;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.util.Locale;

/**
 *	日期时间工具类
 * @author sunyiran
 * @date 2018-12-07
 */
public class DateTimeUtils {


	/**
	 * 根据时间获取时间戳
	 * @param dateTime
	 * @return
	 */
	public static Long dateTimeToTimestamp(LocalDateTime dateTime) {

		if (dateTime == null) {
			return null;
		}
		return dateTime.toInstant(ZoneOffset.ofHours(8)).toEpochMilli();
	}


	/**
	 * 根据时间戳获取日期时间
	 * @param timestamp
	 * @return
	 */
	public static LocalDateTime timestampToDateTime(Long timestamp) {
		return timestamp==null?null:LocalDateTime.ofEpochSecond(timestamp,0,ZoneOffset.ofHours(8));
	}


	/**
	 * 获取指定格式的日期字符串
	 * @param dateTime
	 * @param format 为null或者空时默认为yyyy-MM-dd HH:mm:ss
	 * @return
	 */
	public static String dateTimeToString(@NotNull LocalDateTime dateTime, String format) {
		//时间转字符串格式化
		if (StringUtils.isEmpty(format)) {
			format = "yyyy-MM-dd HH:mm:ss";
		}
		DateTimeFormatter formatter = DateTimeFormatter.ofPattern(format,Locale.SIMPLIFIED_CHINESE);
		return dateTime.format(formatter);
	}


	/**
	 * 根据日期时间字符串获取日期时间
	 * @param datetime
	 * @param format 为null或者空时默认为yyyy-MM-dd HH:mm:ss
	 * @return
	 */
	public static LocalDateTime stringToDateTime(@NotNull String datetime,String format) {

		if (StringUtils.isEmpty(format)) {
			format = "yyyy-MM-dd HH:mm:ss";
		}
		DateTimeFormatter formatter = DateTimeFormatter.ofPattern(format);
		return LocalDateTime.parse(datetime,formatter);
	}


	public static void main(String[] args) {
		System.out.println(stringToDateTime("2018-12-07 09:50:33",null));
	}
}

 

  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
LocalDateTime是Java 8中的一个类,用于表示日期和时间。创建LocalDateTime对象的方法有几种。 首先,可以使用now方法创建当前的LocalDateTime对象。这个方法会根据系统默认的时区创建对象。例如,在代码中使用LocalDateTime.now()就可以创建当前的LocalDateTime对象。 其次,可以使用now方法并传入一个特定的时区来创建LocalDateTime对象。比如,使用LocalDateTime.now(ZoneId.of("Asia/Shanghai"))可以创建基于亚洲/上海时区的LocalDateTime对象。同样地,使用LocalDateTime.now(ZoneId.of("America/Los_Angeles"))可以创建基于美国洛杉矶时区的LocalDateTime对象。 另外,可以使用LocalDateTime.parse(String, formatter)方法将一个字符串转换LocalDateTime对象。这个方法需要传入一个DateTimeFormatter实例作为第二个参数,用来指定字符串日期模式。 此外,还可以使用format()方法将一个LocalDateTime对象格式化为字符串。这个方法是非静态方法,需要在一个LocalDateTime对象上调用。使用DateTimeFormatter.ofPattern("dd-MM-yyyy HH:mm")来创建一个DateTimeFormatter实例来指定日期模式,然后使用LocalDateTime对象的format()方法将其格式化为指定模式的字符串。例如:DateTimeFormatter aFormatter = DateTimeFormatter.ofPattern("dd-MM-yyyy HH:mm"); LocalDateTime localDateTime = LocalDateTime.of(2017, Month.AUGUST, 3, 12, 30); String formattedString = localDateTime.format(aFormatter); 总结一下,使用now方法可以创建当前的LocalDateTime对象,也可以传入一个特定的时区来创建。使用parse方法可以将字符串转换LocalDateTime对象,使用format方法可以将LocalDateTime对象格式化为字符串。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [LocalDateTime基本使用](https://blog.csdn.net/weixin_44062339/article/details/113799915)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* *3* [如何在Java 8中使用LocalDateTime格式化/解析日期-示例教程](https://blog.csdn.net/dnc8371/article/details/106702406)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值