一. joda-time 基础解释
- joda-time 是一个流行的 Java 日期和时间处理库,在 Golang 中有对应的实现 go-joda. 它提供了对日期、时间、时区、周期、持续时间、格式化和解析等多个时间操作的支持,设计得非常全面和庞大。它的优点如下:
- 支持多种日历系统,包括 ISO、Julian、Coptic 和 Ethiopic 等。
- 提供了丰富的日期和时间操作方法,并提供了更加灵活的时间格式化和解析方法,支持任意格式的时间字符串。
- 支持日期和时间的比较、加减运算、取整等常用操作,方便进行复杂的时间计算。
- 支持时区和夏令时的处理,可以自动识别夏令时变化,转换成 UTC 时间等。
- 由于 joda-time 设计得非常庞大,可能会过于复杂和繁琐,不太适合简单的时间操作
- 使用示例
package main
import (
"fmt"
"github.com/vjeantet/jodaTime"
)
func main() {
now := jodaTime.Format("YYYY-MM-dd HH:mm:ss", jodaTime.Now())
time, _ := jodaTime.Parse("YYYY-MM-dd HH:mm:ss", "2023-05-24 01:32:25")
plus1day := jodaTime.PlusDays(time, 1)
minus30mins := jodaTime.MinusMinutes(time, 30)
fmt.Println("当前时间:", now)
fmt.Println("解析后的时间:", jodaTime.Format("YYYY-MM-dd HH:mm:ss", time))
fmt.Println("增加一天后的时间:", jodaTime.Format("YYYY-MM-dd HH:mm:ss", plus1day))
fmt.Println("减少30分钟后的时间:", jodaTime.Format("YYYY-MM-dd HH:mm:ss", minus30mins))
}
二. joda-time常用API简介
func Now() *time.Time
func Parse(layout, value string) (*time.Time, error)
func Format(layout string, time *time.Time) string
func GetYear(time *time.Time) int
func GetMonth(time *time.Time) int
func GetMonthDays(time *time.Time) int
func GetWeekday(time *time.Time) int
func FirstDayOfMonth(time *time.Time) *time.Time
func LastDayOfMonth(time *time.Time) *time.Time
func FirstDayOfWeek(time *time.Time) *time.Time
func LastDayOfWeek(time *time.Time) *time.Time
func FirstDayOfYear(time *time.Time) *time.Time
func LastDayOfYear(time *time.Time) *time.Time
func PlusDays(time *time.Time, days int) *time.Time
func PlusHours(time *time.Time, hours int) *time.Time
func PlusMinutes(time *time.Time, minutes int) *time.Time
func PlusSeconds(time *time.Time, seconds int) *time.Time
func MinusDays(time *time.Time, days int) *time.Time
func MinusHours(time *time.Time, hours int) *time.Time
func MinusMinutes(time *time.Time, minutes int) *time.Time
func MinusSeconds(time *time.Time, seconds int) *time.Time
func IsAfter(time1, time2 *time.Time) bool
func IsBefore(time1, time2 *time.Time) bool
func Equals(time1, time2 *time.Time) bool
func DurationBetween(start, end *time.Time) int64
func DaysBetween(start, end *time.Time) int
func WeeksBetween(start, end *time.Time) int
func MonthsBetween(start, end *time.Time) int
func YearsBetween(start, end *time.Time) int
func Plus(time *time.Time, durationType DurationType, duration int) *time.Time
func Minus(time *time.Time, durationType DurationType, duration int) *time.Time
func GetTimeZone(id string) *time.Location
func ConvertToTimeZone(time *time.Time, timeZone *time.Location) *time.Time
func ConvertToUTC(time *time.Time) *time.Time
func ConvertToLocal(time *time.Time) *time.Time
func GetMidnight(time *time.Time) *time.Time
func IsLeapYear(year int) bool
func GetFirstDayOfWeekInMonth(time *time.Time, weekNum int) *time.Time
func GetLastDayOfWeekInMonth(time *time.Time, weekNum int) *time.Time
func IsWeekday(time *time.Time) bool
func IsWeekend(time *time.Time) bool
func GetHours(time *time.Time, is24hr bool) int
func GetMinutes(time *time.Time) int
func GetSeconds(time *time.Time) int
func FormatWithTimeZone(time *time.Time, pattern string, timeZone *time.Location) string
func ParseWithTimeZone(dateString, pattern string, timeZone *time.Location) (*time.Time, error)
三. 其它三方时间库
- Golang 标准库 time 可以执行基本的时间操作,这个库非常简单易用,也适用于大多数的时间场景
- go-carbon 是一个开源的 Golang 时间处理库,提供了更为高效和精确的时间计算能力,但是的功能相对简单,不支持复杂的时间操作,例如月份的差异计算等
- dateparse 是 Golang 中一个轻量级的日期时间格式化和解析库,相比于标准库 time 和 joda-time,它更加灵活和易用,但是 功能较为单一,不支持复杂的时间计算和操作,适用于简单的时间格式化和解析需求