时间日期格式化----链式调用

        在java代码的编写中规范代码很重要,有的人写代码比较随意,天马行空,想到什么就写什么,往往是一个大的逻辑下来就写一个方法,亦或是写少许几个方法,这样一来,为实现一些简单的功能,不得不在一个方法中写上几十行甚至是几百行,这样,不仅仅代码逻辑不清晰还容易出错,即便是出现bug往往解决起来也非常的头疼,因为解决bug的前提是先理清业务逻辑,然后才能改bug,但是有的项目本身业务逻辑就不是很清晰,我指的是写在纸上的,所以这样很容易就使得项目本身变成了一个坑,有时候貌似很稳定,但是谁动谁死.

        在IT业的那些大牛看来,写代码不仅仅是要实现功能,而且更重要的是在代码逻辑严谨的情况下提高代码的复用度,而为了使逻辑严谨,不容易出错,最简单有效的方法就是缩短代码的行数,一个方法往往只写两三行,通过实现代码的简洁进而实现代码的严谨,这样一来不可避免的会增加方法的数量,而通过提高代码的复用度正好可以缩小代码的写作量.

        通过这种方法,我们不仅仅可以使得代码更加简洁,逻辑更加严谨,而且能够有效的提高编程效率,整个项目下来,所编写的代码反而倒更加的小巧.

        示例代码如下:

package net.sahv.yrsy.util.service;

public interface DateService {
	
	/**
	 * @param timeZoneId
	 * 初始化当前时间--用户可以自定义时区
	 * @return DateService
	 */
	DateService getTimeZone(String timeZoneId);
	/**
	 * 初始化当前时区,默认是GMT时区
	 * @return DateService
	 */
	DateService getTimeZone();
	/**
	 * 初始化当前时间,默认是2017-02-28格式
	 * @return DateService
	 */
	DateService getCurrentDate();
	/**
	 * @param formatString
	 * 初始化当前时间--用户可以自定义时区
	 * @return DateService
	 */
	DateService getCurrentDate(String formatString);
	/**
	 * 将日期字符串分割成字符串数组 
	 * @return DateService
	 */
	DateService changeTODateArray();
	/**
	 * 获取当前的日期字符串
	 * @return 返回String类型的数据
	 */
	String getDateString();
	/**
	 * 获取拆分的日期数组
	 * @return String[]
	 */
	String[] getDateArray();
}
package net.sahv.yrsy.util.service.serviceImpl;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

import net.sahv.yrsy.util.service.DateService;

public class DateServiceImpl implements DateService{
	
	private Calendar calendar=null;
	private String dateString=null;
	private String[] dateArray=null;
	
	/**
	 * 初始化当前时间--用户可以自定义时区
	 */
	@Override
	public DateService getTimeZone(String timeZoneId) {
		TimeZone zone=TimeZone.getTimeZone(timeZoneId);
		this.calendar=Calendar.getInstance(zone);
		return this;
	}

	/**
	 * 初始化当前时区
	 */
	@Override
	public DateService getTimeZone() {
		if(calendar==null){
			this.calendar=Calendar.getInstance();
		}
		return this;
	}

	/**
	 * 获取当前时间的字符串
	 */
	@Override
	public DateService getCurrentDate() {
		//日期格式化
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
		//获取当前时间
		Date date=calendar.getTime();
		//格式化当前时间为字符串
		this.dateString=sdf.format(date);
		return this;
	}

	/**
	 * 将时间字符串格式化成一个整形数组默认是用"-"来进行分割
	 */
	@Override
	public DateService getCurrentDate(String formatString) {
		SimpleDateFormat sdf=new SimpleDateFormat(formatString);
		//获取当前时间
		Date date=calendar.getTime();
		//格式化当前时间为字符串
		this.dateString=sdf.format(date);
		return this;
	}

	/**
	 * 将日期字符串分割成字符串数组 
	 */
	@Override
	public DateService changeTODateArray() {
		this.dateArray=dateString.split("-");
		return this;
	}

	/**
	 * 获取当前的日期字符串
	 */
	@Override
	public String getDateString() {
		return dateString;
	}

	/**
	 * 获取拆分的日期数组
	 */
	@Override
	public String[] getDateArray() {
		return dateArray;
	}

}
package net.sahv.yrsy.test;

import net.sahv.yrsy.util.service.DateService;
import net.sahv.yrsy.util.service.serviceImpl.DateServiceImpl;

/**
 * 测试时间类
 */
public class TestDate {

	public static void main(String[] args) {
		
		DateService dateService=new DateServiceImpl();
		DateService currentDate=dateService.getTimeZone().getCurrentDate().changeTODateArray();
		String[] dateArray=currentDate.getDateArray();
		for(int i=0;i
   
   

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值