java-时间

一、Java时间互转工具类

public static String farmat(Date date, String fromat) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(fromat);
        return simpleDateFormat.format(date);
    }

    public static String farmat(Date date) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
        return simpleDateFormat.format(date);
    }

    public static String DateToStr(Date date) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return format.format(date);
    }

    public static String DateToStr2(Date date) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        return format.format(date);
    }

    public static Date StrToDate(String str) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Date date = null;
        try {
            date = format.parse(str);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return date;
    }


    public static String timeDifference(String a1, String b1) {
        DateTimeFormatter forPattern = DateTimeFormat.forPattern("yyyy-MM-dd");
        DateTime a2 = forPattern.parseDateTime(a1);
        DateTime b2 = forPattern.parseDateTime(b1);
        int months = Months.monthsBetween((ReadableInstant) b2, (ReadableInstant) a2).getMonths();
        return months + "";
    }


    public static String randomDate(String beginDate, String endDate) {
        try {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            Date start = simpleDateFormat.parse(beginDate);
            Date end = simpleDateFormat.parse(endDate);
            if (start.getTime() >= end.getTime()) {
                return null;
            }

            long data = random(start.getTime(), end.getTime());
            return DateToStr2(new Date(data));
        } catch (Exception exception) {


            return null;
        }
    }

    public static long random(long begin, long end) {
        long rtn = begin + (long) (Math.random() * (end - begin));
        if (rtn == begin || rtn == end) {
            return random(begin, end);
        }
        return rtn;
    }

二、实现计算两个日期的月份差实例代码

1. Maven导入依赖:

 <dependency>
	<groupId>joda-time</groupId>
		<artifactId>joda-time</artifactId>
	<version>2.9.6</version>
</dependency>

2. 计算过程

package com.forezp.util;
import org.joda.time.DateTime;
import org.joda.time.Months;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
/**
* 在JAVA中,如何计算两个日期的月份差?<br>
*
*
* @author Administrator
*
*/
public class Demo1 {
	public static void main(String[] args) {
		DateTimeFormatter formatter = DateTimeFormat.forPattern("yyyy-MMdd");
		DateTime start = formatter.parseDateTime("2017-07-11");
		DateTime end = formatter.parseDateTime("2017-11-19");
		int months = Months.monthsBetween(start, end).getMonths();
		System.out.println(months);
	}
}

三、java生成指定时间内的随机时间

 public static void main(String[] args) {
 
        for (int i=0;i<30;i++){
            Date date = randomDate("2019-01-01","2019-01-31");
            System.out.println(new SimpleDateFormat("yyyy.MM.dd HH:mm:ss").format(date));
        }
    }
    private static Date randomDate(String beginDate,String endDate){
        try {
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            Date start = format.parse(beginDate);
            Date end = format.parse(endDate);
 
            if(start.getTime() >= end.getTime()){
                return null;
            }
            long date = random(start.getTime(),end.getTime());
            return new Date(date);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
 
    private static long random(long begin,long end){
        long rtn = begin + (long)(Math.random() * (end - begin));
        if(rtn == begin || rtn == end){
            return random(begin,end);
        }
        return rtn;
    }
 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值