1、时间转换的问题,将字符串转换成日期
String s = “20190112102230”;
DateTimeFormatter df = DateTimeFormatter.ofPattern(“yyyyMMddHHmmss”).withZone(ZoneId.systemDefault());
ZonedDateTime time = ZonedDateTime.parse(s, df);
OffsetDateTime dateTime = time.toOffsetDateTime();
System.out.println(dateTime);
不能直接转为OffsetDateTime ,因为没有时间偏移
String s = “20190112102230”;
DateTimeFormatter df = DateTimeFormatter.ofPattern(“yyyyMMddHHmmss”).withZone(ZoneId.systemDefault());
LocalDateTime time = LocalDateTime.parse(s, df);
ZonedDateTime zoneTime = time.atZone(ZoneId.systemDefault());
OffsetDateTime dateTime = zoneTime.toOffsetDateTime();
System.out.println(dateTime);
2、NULL详解
1.null不属于任何类型,可以被转换成任何类型,但是用instanceof永远返回false.
Object obj = null;
Student stu = (Student)obj;
System.out.println(stu);
System.out.println(stu instanceof Student);
2.null永远不能和八大基本数据类型进行赋值运算等,否则不是编译出错,就是运行出错.
这个是因为null是对象类型,不能和原始类型直接运算
3.null可以和字符串进行运算.
Object obj = null;
System.out.println(obj + " ab");
输出:null ab
4.同种类型的null,比较都返回true,null==null也返回true.
三、注解
@Documented–一个简单的Annotations标记注解,表示是否将注解信息添加在java文档中。
@Retention– 定义该注解的生命周期。
RetentionPolicy.SOURCE – 在编译阶段丢弃。这些注解在编译结束之后就不再有任何意义,所以它们不会写入字节码。@Override, @SuppressWarnings都属于这类注解
RetentionPolicy.CLASS – 在类加载的时候丢弃。在字节码文件的处理中有用。注解默认使用这种方式
RetentionPolicy.RUNTIME– 始终不会丢弃,运行期也保留该注解,因此可以使用反射机制读取该注解的信息。我们自定义的注解通常使用这种方式
@Target – 表示该注解用于什么地方。如果不明确指出,该注解可以放在任何地方。以下是一些可用的参数。需要说明的是:属性的注解是兼容的,如果你想给7个属性都添加注解,仅仅排除一个属性,那么你需要在定义target包含所有的属性
ElementType.TYPE:用于描述类、接口或enum声明
ElementType.FIELD:用于描述实例变量
ElementType.METHOD
ElementType.PARAMETER
ElementType.CONSTRUCTOR
ElementType.LOCAL_VARIABLE
ElementType.ANNOTATION_TYPE 另一个注释
ElementType.PACKAGE 用于记录java文件的package信息
@Inherited – 定义该注释和子类的关系
例子:
创建一个注解
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(value = { ElementType.METHOD })
public @interface LoginCheck {
}
创建一个切面
@Aspect
@Component
public class LoginAop {
@Autowired
private com.springtest.spring_redission.service.LoginService loginService;
@Pointcut("@annotation(com.springtest.spring_redission.anno.LoginCheck)")
public void annotationPointCut() {
}
@Before("annotationPointCut()")
public void before(JoinPoint joinPoint) {
loginService.loginCheck();
}
}
这个注解要调用的功能
@Service
public class LoginService {
public void loginCheck() {
System.out.println("check user login info...");
}
}
使用注解
@RestController
public class TestController {
@GetMapping("/test2")
@LoginCheck
public String testAnno() {
System.out.println("-------testAnno----------");
return "sucess";
}
}
启动项目,浏览器输入http://localhost:3300/test2