Java的功能记录

本文详细介绍了如何在Java中将字符串转换为日期时间,包括处理时区和偏移量,以及对NULL值的深入解析。同时,深入探讨了Java注解的使用,包括@Documented、@Retention、@Target等元注解,并通过具体示例展示了如何创建自定义注解并应用于方法级。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值