错误:
java.lang.IllegalArgumentException: Cannot format given Object as a Date
源码:
firstVisitState = getRuntimeContext().getState(new ValueStateDescriptor<String>("firstVisitState", String.class));
sdf = new SimpleDateFormat("yyyy-MM-dd");
//状态中保存的是字符串,直接用format格式化,很容易报错.
String yesterday = sdf.format(firstVisitState.value() == null ? 0L : firstVisitState.value());
错误原因分析:
1.format函数只能传入Date数据类型 输出为String类型
2.parse函数输入为String类型,输出为Date类型.
这里状态中保存的是字符串,不能直接用format函数.
错误解决:
firstVisitState = getRuntimeContext().getState(new ValueStateDescriptor<String>("firstVisitState", String.class));
sdf = new SimpleDateFormat("yyyy-MM-dd");
//将状态中的String转为Date类型,再用format即可.
Date date = sdf.parse(firstVisitState.value() == null ? "1970-01-01 00:00:00" : firstVisitState.value());
String yesterday = sdf.format(date);