业务需要的时间
- OrderDate 注文日
- Trade Date 约定日(约定日与受付日之间有两个工作日,去掉节假日 t+2)
- Settlement Date 受付日
业务需要
通常将格式化的字符串定义成常量或者枚举
常量
private static final String FORMATDATE = "yyyy-MM-dd hh:mm:ss";
枚举
package com.example.demo.BeanUtils;
/**
* @author shuyue.guo
* @date 2019/12/20
*/
public class DateEnum {
public static enum DateFormat{
/**
* format
*/
ONE("a","yyyy-MM-dd hh:mm:ss"),
TWO("b","yyyy/MM/dd hh:mm:ss"),
;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getFormat() {
return format;
}
public void setFormat(String format) {
this.format = format;
}
private String name,format;
DateFormat(String name, String format) {
this.name = name;
this.format = format;
}
}
}
使用枚举格式化
LocalDateTime localDateTime = LocalDateTime.now();
System.out.println(localDateTime);//2019-12-20T15:32:03.402769700
StringformDate=
localDateTime.format(DateTimeFormatter.ofPattern(DateEnum.DateFormat.TWO.getFormat()));
System.out.println(formDate);//2019/12/20 03:32:03
System.out.println(DateEnum.DateFormat.TWO.getFormat());//yyyy/MM/dd hh:mm:ss
格式化
String formatDate = localDateTime2.format(DateTimeFormatter.ofLocalizedDateTime(FormatStyle.MEDIUM));
String localDateString = localDateTime2.format(DateTimeFormatter.ISO_DATE);
// 2015-01-25
String formatDate = localDateTime2.format(DateTimeFormatter.ofLocalizedDateTime(FormatStyle.MEDIUM));
String date_s = "2014-12-31";
SimpleDateFormat dt = new SimpleDateFormat("yyyy-MM-dd");
java.util.Date d = dt.parse(date_s);
System.out.println(d);//Wed Dec 31 00:00:00 CST 2014
SimpleDateFormat dt1 = new SimpleDateFormat("YYYY");
System.out.println(dt1.format(d));//2015
sql | java8 |
---|---|
DATE() | LocalDate |
TIME() | LocalTime |
TIMESTAMP | LocalDate Time |
offsetDate | |
offsetDateTime |
-
offsetTime and ZonedDateTime
我们总是应该优先选择将OffsetDateTime存储在数据库中,而不是ZonedDateTime上,因为具有本地时间偏移量的日期始终表示同一时刻。 -
时间戳字段(timestamp)
时间戳。TIMESTAMP 值使用 Unix 纪元(‘1970-01-01 00:00:00’ UTC) 至今的描述来存储。格式:YYYY-MM-DD HH:MM:SS
支持的范围是从 ‘1970-01-01 00:00:01’ UTC 到 ‘2038-01-09 03:14:07’ UTC
mysql 时间戳与日期格式的相互转换 -
前后端分离时,后端生成的API需要带时区信息
-
日期和时间
1、在JavaScript中时间戳指的是当前时间到1970年1月1日00:00:00 UTC对应的毫秒数,和unix时间戳不是一个概念,后者表示秒数,差了1000倍。
2、new Date(timestamp)中的时间戳必须是number格式,string会返回Invalid Date。所以比如new Date(‘11111111’)这种写法是错的。
3、 处理时区与日期的工具库
4、时区处理
momentjs 库
function myDate() {
$.ajax({
url: "http://127.0.0.1:8083/gsy/date",
method: 'post',
success: function (data) {
console.log(data.localDate); //2019-12-23T16:10:47.0969608
console.log(data.zoneId); //Asia/Tokyo
var japanDate = moment(data.localDate).tz(data.zoneId).format('YYYY-MM-DD HH:mm:ss');
console.log(japanDate); //2019-12-23 17:11:46
}
})
}
myDate();
@RequestMapping("/date")
@ResponseBody
public Object returnDate() {
LocalDateTime localDateTime = LocalDateTime.now();
ZoneId zoneId = ZoneId.of("Asia/Tokyo");
HashMap<String,Object> map = new HashMap<>();
map.put("localDate",localDateTime);
map.put("zoneId",zoneId);
return map;
}
var time = new Date(2018, 01, 01, 10, 0);//客户端
function getTruthTime(time) {
// 服务器端时区,北京东八区(-8*60)
let timezone = -480
// 客户端实际时区(例如东京为东九区:-540)
//new Date 服务器端
// let offsetGMT = new Date().getTimezoneOffset();
let offsetGMT = -540;
console.log(offsetGMT)//-540
// 计算差值(用户在东京时区下下单,需要补回1小时)
let adjust = timezone - offsetGMT
console.log(adjust); //60
// 需要修复的时间,支持字符串传参
let nowDate = time ? new Date(time) : new Date()
console.log(new Date(time))//Thu Feb 01 2018 10:00:00 GMT+0800 (中国标准时间)
console.log(nowDate);//Thu Feb 01 2018 10:00:00 GMT+0800 (中国标准时间)
let timeStamp = new Date(time + adjust * 60 * 1000)
return timeStamp
}
console.log(getTruthTime(time)); //Thu Feb 01 2018 10:00:00 GMT+0800 (中国标准时间)
客户端输入日期
- 控件 几个日期控件
- 日期验证(api,正则验证)
BIgDecimal 货币处理
-
数据类型
BigDecimal -
在DB中NUMBR(11,2)存储
- NUMBER(精度,小数位数)
NUMBER(precision, scale) - DECIMAL(p,s) 精确数值,精度 p,小数点后位数 s。例如:decimal(5,2) 是一个小数点前有 3 位数,小数 点后有 2 位数的数字。
- NUMERIC(p,s) 精确数值,精度 p,小数点后位数 s。(与 DECIMAL 相同)
- DECIMAL 和 NUMERIC区别:
NUMERIC必须与定义的精确度完全一样-因此,如果定义4个小数位,则DB必须始终存储4个小数位。
DECIMAL必须至少与定义的精度一样。这意味着数据库实际上可以存储比指定数目更多的数字。
- NUMBER(精度,小数位数)
CREATE TABLE TEST(ID INT PRIMARY KEY,
numc numeric(6,2),
dec decimal(6,2) );
select * from test
insert into test(id,numc,dec) values(1,1234.12,1234.12)
insert into test(id,numc,dec) values(2,1234.12,1235.12)
运行结果:
将dec列插入的有效位数比实际设的有效位数多
insert into test(id,numc,dec) values(3,12345.12,1234.12)
运行结果:
-
端数丸め(四舍五入)
整体运算完成后再进行舍入
-
CEILING
Rounding mode to round towards positive infinity.
向正无穷方向舍入 -
DOWN
Rounding mode to round towards zero.
向零方向舍入 -
FLOOR
Rounding mode to round towards negative infinity.
向负无穷方向舍入 -
HALF_DOWN
Rounding mode to round towards “nearest neighbor” unless both neighbors are equidistant, in which case round down.
向(距离)最近的一边舍入,除非两边(的距离)是相等,如果是这样,向下舍入, 例如1.55 保留一位小数结果为1.5 -
HALF_EVEN
Rounding mode to round towards the “nearest neighbor” unless both neighbors are equidistant, in which case, round towards the even neighbor.
向(距离)最近的一边舍入,除非两边(的距离)是相等,如果是这样,如果保留位数是奇数,使用ROUND_HALF_UP ,如果是偶数,使用ROUND_HALF_DOWN -
HALF_UP
Rounding mode to round towards “nearest neighbor” unless both neighbors are equidistant, in which case round up.
向(距离)最近的一边舍入,除非两边(的距离)是相等,如果是这样,向上舍入, 1.55保留一位小数结果为1.6 -
UNNECESSARY
Rounding mode to assert that the requested operation has an exact result, hence no rounding is necessary.
计算结果是精确的,不需要舍入模式 -
UP
Rounding mode to round away from zero.
向远离0的方向舍入
见API RoundingMode
BigDecimal up = new BigDecimal(1.5);
BigDecimal upScale = up.setScale(0,RoundingMode.UP);//2
setScale第一位表示保留几位小数
BigDecimal upScale = up.setScale(1,RoundingMode.UP);// 1.5
BigDecimal up = new BigDecimal(1.45);
BigDecimal upScale = up.setScale(1,RoundingMode.UP);// 1.5
BigDecimal loanAmount = new BigDecimal("150.48");// 贷款金额
BigDecimal interestRate = new BigDecimal("0.008");// 利率
BigDecimal interest = loanAmount.multiply(interestRate);// 相乘
// 货币格式化
NumberFormat currency = NumberFormat.getCurrencyInstance();
NumberFormat percent = NumberFormat.getPercentInstance();
// 百分比小数点最多3位
percent.setMaximumFractionDigits(3);
System.out.println("贷款金额:\t" + currency.format(loanAmount)); //贷款金额: ¥150.48
System.out.println("利率:\t" + percent.format(interestRate)); //利率: 0.8%
System.out.println("利息:\t" + currency.format(interest)); //利息: ¥1.20
其他国家货币形式
NumberFormat currency1 = NumberFormat.getCurrencyInstance(Locale.JAPANESE);
System.out.println("日币" + currency1.format(loanAmount));//日币¤150.48
equals方法会比较值和精确度,而compareTo则会忽略精度。
BigDecimal x = new BigDecimal("1");
BigDecimal y = new BigDecimal("1.00");
System.out.println(x.equals(y)); // false
System.out.println(x.compareTo(y) == 0 ? "true": "false"); //true
传递额前短的数据类似于
{
"long": 12345678,
"scale": 3
}
前端处理:
console.log(0.1 + 0.2);//0.30000000000000004
JavaScript 里的数字是采用 IEEE 754 标准的 64 位双精度浮点数。该规范定义了浮点数的格式,对于64位的浮点数在内存中的表示,最高的1位是符号位,接着的11位是指数,剩下的52位为有效数字,具体:
第0位:符号位, s 表示 ,0表示正数,1表示负数;
第1位到第11位:储存指数部分, e 表示 ;
第12位到第63位:储存小数部分(即有效数字),f 表示
decimal js库
use decimal js