Java Calendar and SimpleDateFormat 时间模块

Posted 入门到放弃

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java Calendar and SimpleDateFormat 时间模块相关的知识,希望对你有一定的参考价值。

package UtilTest;
import java.util.Calendar;
import java.text.SimpleDateFormat;
import org.apache.commons.lang.RandomStringUtils;

public class TimeTest {
	static String time=null;
	public static void main(String[] args ) {
		System.out.println(getLocaltime());
		System.out.println(getfurTime(0, 0, 0, 0, 2, 0));
		System.out.println(comparetime(getLocaltime(),getfurTime(0, 0, 0, 0, 2, 0)));
		System.out.println(getrdnum());
	}
	public static String getLocaltime() {
		Calendar calendar =Calendar.getInstance();
		time=(new SimpleDateFormat(("yyyy-MM-dd  HH:mm:ss:SS"))).format(calendar.getTime());
		return time;
	}
	public static String  getfurTime(int Y,int M ,int Date,int h,int m,int s) {
		Calendar calendar =Calendar.getInstance();
		calendar.add(Calendar.YEAR,Y);
		calendar.add(Calendar.MONTH, M);
		calendar.add(Calendar.DAY_OF_MONTH, Date);
		calendar.add(Calendar.HOUR_OF_DAY, h);
		calendar.add(Calendar.MINUTE, m);
		calendar.add(Calendar.SECOND, s);
		time=(new SimpleDateFormat(("yyyy-MM-dd  HH:mm:ss:SS"))).format(calendar.getTime());
		return time;
	}
	public static boolean comparetime(String curtime,String furtime) {
		boolean bool=false;
		int res=curtime.compareTo(furtime);
		if(res<=0) {
			bool=true;
		}else if(res>0) {
			bool=false;
		}
		return  bool;
	}
	public static String getrdString() {
		String rd=RandomStringUtils.random(11, true, true);
		return rd;
	}
	public static String getrdnum() {
		String rd=RandomStringUtils.random(0, 2, 3, false, true);
		return rd;
	}
}

  

以上是关于Java Calendar and SimpleDateFormat 时间模块的主要内容,如果未能解决你的问题,请参考以下文章

Java获取指定时间(转)

Java Date and Calendar examples

Java Calendar and SimpleDateFormat 时间模块

简单的Java题目,关于年份、时间、日期的计算。求助!!!

JPA and ( or )

日期工具类