Java:joda-time处理时间对象

简介: Java:joda-time处理时间对象

文档:

https://www.joda.org/joda-time/userguide.html

依赖

<dependency>
     <groupId>joda-time</groupId>
     <artifactId>joda-time</artifactId>
     <version>2.10.5</version>
 </dependency>

示例

package org.example;


import org.joda.time.DateTime;
import org.joda.time.Days;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;

import java.util.Date;

public class JodaTimeTest {
public static void main(String[] args) {
/**
* 获取 DateTime
*/
// 构造函数 现在时间
DateTime dt1 = new DateTime();
System.out.println(dt1);
// 2020-06-21T10:54:11.378+08:00

// 静态方法 现在时间
DateTime dt2 = DateTime.now();
System.out.println(dt2);
// 2020-06-21T10:54:11.444+08:00

// long -> DateTime
DateTime dt3 = new DateTime(1609294356000L);
System.out.println(dt3);
// 2020-12-30T10:12:36.000+08:00

// int -> DateTime
DateTime dt4 = new DateTime(2020, 1, 12, 12, 30, 53);
System.out.println(dt4);
// 2020-01-12T12:30:53.000+08:00

// Date -> DateTime
DateTime dt5 = new DateTime(new Date());
System.out.println(dt5);
// 2020-06-21T10:47:01.461+08:00

// 解析时间 String -> DateTime
DateTimeFormatter formatter = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss");
DateTime dt6 = DateTime.parse("2020-12-30 10:12:36", formatter);
System.out.println(dt6);
// 2020-12-30T10:12:36.000+08:00

/**
* DateTime 转为其他对象
*/
// 格式化时间 DateTime -> String
System.out.println(dt6.toString("yyyy-MM-dd HH:mm:ss"));
// 2020-12-30 10:12:36

// 获取13位时间戳 DateTime -> Long
System.out.println(dt6.getMillis());
// 1609294356000

// DateTime -> int
System.out.println(dt6.getYear());
System.out.println(dt6.getMonthOfYear());
System.out.println(dt6.getDayOfMonth());
System.out.println(dt6.getHourOfDay());
System.out.println(dt6.getMinuteOfHour());
System.out.println(dt6.getSecondOfMinute());
System.out.println(dt6.getMillisOfSecond());
// 2020 12 30 10 12 36 0

// DateTime -> Date
System.out.println(dt6.toDate());
// Wed Dec 30 10:12:36 CST 2020

/**
* 时间修改 with开头
*/
DateTime dt7 = dt6.withYear(1)
.withMonthOfYear(2)
.withDayOfMonth(3)
.withHourOfDay(4)
.withMinuteOfHour(5)
.withSecondOfMinute(6)
.withMillisOfSecond(7);

System.out.println(dt7);
// 0001-02-03T04:05:06.007+08:05:43

/**
* 时间计算 plus(+) 或者 minus(-)开头
*/

DateTime dt8 = dt7.plusYears(7)
.plusMonths(6)
.plusDays(5)
.plusHours(4)
.plusMinutes(3)
.plusSeconds(2
.plusMillis(1);

System.out.println(dt8);
// 0008-08-08T08:08:08.008+08:05:43

/**
* 计算两个时间间隔天数
*/

DateTime dt9 = new DateTime(2020, 10, 20, 10, 30, 12);
DateTime dt10 = new DateTime(2020, 10, 30, 20, 30, 12);
System.out.println(Days.daysBetween(dt9, dt10).getDays());
// 10
}
}


            </div>
目录
相关文章
uniapp上传文件时用到的api是什么?格式是什么?
uniapp上传文件时用到的api是什么?格式是什么?
|
9天前
|
数据采集 人工智能 安全
|
4天前
|
机器学习/深度学习 人工智能 前端开发
构建AI智能体:七十、小树成林,聚沙成塔:随机森林与大模型的协同进化
随机森林是一种基于决策树的集成学习算法,通过构建多棵决策树并结合它们的预测结果来提高准确性和稳定性。其核心思想包括两个随机性:Bootstrap采样(每棵树使用不同的训练子集)和特征随机选择(每棵树分裂时只考虑部分特征)。这种方法能有效处理大规模高维数据,避免过拟合,并评估特征重要性。随机森林的超参数如树的数量、最大深度等可通过网格搜索优化。该算法兼具强大预测能力和工程化优势,是机器学习中的常用基础模型。
305 164
|
3天前
|
机器学习/深度学习 自然语言处理 机器人
阿里云百炼大模型赋能|打造企业级电话智能体与智能呼叫中心完整方案
畅信达基于阿里云百炼大模型推出MVB2000V5智能呼叫中心方案,融合LLM与MRCP+WebSocket技术,实现语音识别率超95%、低延迟交互。通过电话智能体与座席助手协同,自动化处理80%咨询,降本增效显著,适配金融、电商、医疗等多行业场景。
318 155
|
12天前
|
SQL 自然语言处理 调度
Agent Skills 的一次工程实践
**本文采用 Agent Skills 实现整体智能体**,开发框架采用 AgentScope,模型使用 **qwen3-max**。Agent Skills 是 Anthropic 新推出的一种有别于mcp server的一种开发方式,用于为 AI **引入可共享的专业技能**。经验封装到**可发现、可复用的能力单元**中,每个技能以文件夹形式存在,包含特定任务的指导性说明(SKILL.md 文件)、脚本代码和资源等 。大模型可以根据需要动态加载这些技能,从而扩展自身的功能。目前不少国内外的一些框架也开始支持此种的开发方式,详细介绍如下。
873 6

热门文章

最新文章