# 说明

用于格式化时间,效率很高,线程安全

# 使用案例

let df = require('date-formatter').of('yyyy-M-d HH:mm:ss')

// 格式化时间
df.format()	// 输出2022-10-1 07:08:09, 输出现在时间的格式化文本
df.format(new Date())	// 输出2023-10-1 07:08:09, 输出指定时间的格式化文本

let date = df.parse('2022-10-1 07:08:09') // 解析时间文本到 date 对象

# 源码

/*JavaScript*/

let ZoneId = require('java.time.ZoneId')
let DateTimeFormatter = require('java.time.format.DateTimeFormatter')
let LocalDateTime = require('java.time.LocalDateTime')
let Instant = require('java.time.Instant')

function of(pattern) {
	return new DateFormatter(pattern)
}

function DateFormatter(pattern) {
	if (pattern == undefined || (typeof pattern != 'string')) {
		pattern = 'yyyy-MM-dd HH:mm:ss'
	}
	// this.pattern = pattern
	this.dateTimeFormatter = DateTimeFormatter.ofPattern(pattern);
	this.zoneId = ZoneId.systemDefault()
}
  
DateFormatter.prototype.format = function(date) {
	if (date instanceof Date) {
		return LocalDateTime.ofInstant(Instant.ofEpochMilli(date.getTime()), this.zoneId).format(this.dateTimeFormatter)
	} else {
		return LocalDateTime.now().format(this.dateTimeFormatter)
	}
}

DateFormatter.prototype.parse = function(text) {
	if (typeof text === 'string') {
		// let timestamp = LocalDateTime.parse(text, this.dateTimeFormatter).atZone(this.zoneId).toInstant().toEpochMilli(); return new Date(timestamp.doubleValue())
		return new Date(LocalDateTime.parse(text, this.dateTimeFormatter).atZone(this.zoneId).toInstant().toEpochMilli().doubleValue())
	}
}

module.exports = {
	of
}