首页 文章 精选 留言 我的

精选列表

搜索[Java],共10000篇文章
优秀的个人博客,低调大师

Java计算两个日期相差的月数

/** * 获取两个日期相差的月数 */ public static int getMonthDiff(Date d1, Date d2) { Calendar c1 = Calendar.getInstance(); Calendar c2 = Calendar.getInstance(); c1.setTime(d1); c2.setTime(d2); int year1 = c1.get(Calendar.YEAR); int year2 = c2.get(Calendar.YEAR); int month1 = c1.get(Calendar.MONTH); int month2 = c2.get(Calendar.MONTH); int day1 = c1.get(Calendar.DAY_OF_MONTH); int day2 = c2.get(Calendar.DAY_OF_MONTH); // 获取年的差值 int yearInterval = year1 - year2; // 如果 d1的 月-日 小于 d2的 月-日 那么 yearInterval-- 这样就得到了相差的年数 if (month1 < month2 || month1 == month2 && day1 < day2) { yearInterval--; } // 获取月数差值 int monthInterval = (month1 + 12) - month2; if (day1 < day2) { monthInterval--; } monthInterval %= 12; int monthsDiff = Math.abs(yearInterval * 12 + monthInterval); return monthsDiff; }

优秀的个人博客,低调大师

Java描述设计模式(07):适配器模式

本文源码:GitHub·点这里 || GitEE·点这里 一、适配器模式简介 1、基础概念 适配器模式把一个类的接口变换成客户端所期待的另一种接口,从而使原本因接口不匹配而无法在一起工作的两个类能够在一起工作。适配器模式有类适配器模式和对象适配器模式,以及缺省(接口)适配器,三种不同的形式。 2、生活场景 基于适配器模式,把220V的电压,转换为需要的110V电压。 public class C01_InScene { public static void main(String[] args) { CurrentAdapter adapter = new CurrentAdapter() ; System.out.println(adapter.get110VCurrent()) ; } } // 220V电流 class Current220V { public int get220VCurrent (){ return 220 ; } } // 110V电流接口 interface Current110V { int get110VCurrent () ; } // 电流适配器 class CurrentAdapter extends Current220V implements Current110V { // 电流转换方法 @Override public int get110VCurrent() { int high = get220VCurrent() ; int low = high/2 ; return low ; } } 二、类适配器 1、模式简介 类的适配器模式把适配的类的API转换成为目标类的API。 2、核心角色 目标(Target)角色 这就是所期待得到的接口。 源(Adapee)角色: 现在需要适配的接口。 适配器(Adaper)角色 适配器类是本模式的核心。适配器把源接口转换成目标接口。 3、源码实现 interface Target { void sampleOperation1(); void sampleOperation2(); } class Adaptee { public void sampleOperation1(){ System.out.println("Adaptee.sampleOperation1()"); } } class Adapter extends Adaptee implements Target{ @Override public void sampleOperation2() { System.out.println("Adapter.sampleOperation2()"); } } 三、对象适配器 1、模式简介 与类的适配器模式一样,对象的适配器模式把被适配的类的API转换成为目标类的API,与类的适配器模式不同的是,对象的适配器模式不是使用继承关系连接到Adaptee类,而是使用委派关系连接到Adaptee类。 2、源码实现 interface Target1 { void sampleOperation1(); void sampleOperation2(); } class Adaptee1 { public void sampleOperation1(){ System.out.println("Adaptee.sampleOperation1()"); } } class Adapter1 implements Target1 { private Adaptee1 adaptee ; public Adapter1 (Adaptee1 adaptee){ this.adaptee = adaptee; } public void sampleOperation1() { this.adaptee.sampleOperation1(); } @Override public void sampleOperation2() { System.out.println("Adapter.sampleOperation2()"); } } 四、接口适配器 1、模式简介 缺省(接口)适配(Default Adapter)模式为一个接口提供缺省实现,这样子类型可以从这个缺省实现进行扩展,而不必从原有接口进行扩展。 2、源代码实现 public class C04_AdapterInte { public static void main(String[] args) { ServiceAdapter adapter = new ServiceAdapter(){ @Override public int serviceOperation2() { return 22 ; } }; System.out.println(adapter.serviceOperation2()); } } interface AbstractService { void serviceOperation1(); int serviceOperation2(); String serviceOperation3(); } class ServiceAdapter implements AbstractService{ @Override public void serviceOperation1() { } @Override public int serviceOperation2() { return 0; } @Override public String serviceOperation3() { return null; } } 五、Spring框架应用 1、应用场景描述 在SpringMvc执行控制执行请求的时候,有这样一个流程 1)前段控制器DispatcherServlet调用处理器适配器去执行Handler(也就是Controller);2)处理器适配器去执行Handler,给适配器返回ModelAndView ;3)处理器适配器向前端控制器返回ModelAndView ; 2、流程分析 核心接口和实现 Controller和HandlerAdapter两核心接口。 HandlerAdapter 适配器接口,使Handler有对应的适配器实现类,适配器代替Handler(控制层Controller)执行相应的方法。 public interface HandlerAdapter { // 判断类型是否匹配 boolean supports(Object var1); // 执行方法,返回ModelAndView ModelAndView handle(HttpServletRequest var1, HttpServletResponse var2, Object var3) throws Exception; } supports()方法传入处理器,判断适配器是否支持,如果支持则返回支持的适配器实现类。 DispatchServlert 抽取源码中体现流程的几个步骤。 protected void doDispatch(HttpServletRequest request, HttpServletResponse response) throws Exception { HandlerExecutionChain mappedHandler = null; mappedHandler = this.getHandler(processedRequest); HandlerAdapter ha = this.getHandlerAdapter(mappedHandler.getHandler()); mv = ha.handle(processedRequest, response, mappedHandler.getHandler()); mappedHandler.applyPostHandle(processedRequest, response, mv); } SimpleControllerHandlerAdapter 最后看下supports和handle两个方法的具体实现。 public class SimpleControllerHandlerAdapter implements HandlerAdapter { public SimpleControllerHandlerAdapter() { } public boolean supports(Object handler) { return handler instanceof Controller; } public ModelAndView handle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { return ((Controller)handler).handleRequest(request, response); } } 六、适配器优缺点 1、优点分析 更好的复用性,系统需要使用现有的类,而此类的接口不符合系统的需要。那么通过适配器模式就可以让这些功能得到更好的复用。更好的扩展性。 2、缺点分析 过多的使用适配器,会让系统非常零乱,不易整体进行把控。 七、源代码地址 GitHub·地址 https://github.com/cicadasmile/model-arithmetic-parent GitEE·地址 https://gitee.com/cicadasmile/model-arithmetic-parent

资源下载

更多资源
Mario

Mario

马里奥是站在游戏界顶峰的超人气多面角色。马里奥靠吃蘑菇成长,特征是大鼻子、头戴帽子、身穿背带裤,还留着胡子。与他的双胞胎兄弟路易基一起,长年担任任天堂的招牌角色。

Nacos

Nacos

Nacos /nɑ:kəʊs/ 是 Dynamic Naming and Configuration Service 的首字母简称,一个易于构建 AI Agent 应用的动态服务发现、配置管理和AI智能体管理平台。Nacos 致力于帮助您发现、配置和管理微服务及AI智能体应用。Nacos 提供了一组简单易用的特性集,帮助您快速实现动态服务发现、服务配置、服务元数据、流量管理。Nacos 帮助您更敏捷和容易地构建、交付和管理微服务平台。

Spring

Spring

Spring框架(Spring Framework)是由Rod Johnson于2002年提出的开源Java企业级应用框架,旨在通过使用JavaBean替代传统EJB实现方式降低企业级编程开发的复杂性。该框架基于简单性、可测试性和松耦合性设计理念,提供核心容器、应用上下文、数据访问集成等模块,支持整合Hibernate、Struts等第三方框架,其适用范围不仅限于服务器端开发,绝大多数Java应用均可从中受益。

WebStorm

WebStorm

WebStorm 是jetbrains公司旗下一款JavaScript 开发工具。目前已经被广大中国JS开发者誉为“Web前端开发神器”、“最强大的HTML5编辑器”、“最智能的JavaScript IDE”等。与IntelliJ IDEA同源,继承了IntelliJ IDEA强大的JS部分的功能。

用户登录
用户注册