Time格式有关

import java.text.ParseException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Locale;


public class TimeAndDate {
	public static void main(String args[]){
		
	}
	
	/**
	 * 获取当前时间
	 * 返回时间类型yyyy-MM-dd HH:mm:ss
	 */
	public static Date getNowTime(){
		Date currentTime = new Date();
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String timeString = sdf.format(currentTime);
		ParsePosition pos = new ParsePosition(0);
		Date currentTime2= sdf.parse(timeString,pos);
		return currentTime2;
	}
	
	/**
	 * 获取当前时间
	 * 返回字符串类型yyyy-MM-dd HH:mm:ss
	 */
	public static String getNowTimeString(){
		Date  currentTime = new Date();
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String timeString= sdf.format(currentTime);
		return timeString;
	}
	 
	/**
	 * 获取当前时间
	 */
	public static Date getNowTimeDate(){
		Date currentTimeDate =new Date();
		return currentTimeDate;
	}
	
	/**
	 * 返回本月(最前一天和)最后一天
	 */
	public static Date getLastDate(){
		Calendar cal = Calendar.getInstance();
		int maxDate=cal.getActualMaximum(Calendar.DATE);
	 // int minDate=cal.getActualMinimum(5);
		cal.set(Calendar.DATE, maxDate);
		Date date=cal.getTime();
		return date;		
	}
	
	/**
	 * 返回一个月(最前一天和)最后一天
	 */
	public static Date getLastDate2(String dateString){
		 SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
		 ParsePosition pos = new ParsePosition(8);
	     Date date=sdf.parse(dateString,pos);
	     Calendar cal = Calendar.getInstance();
	     cal.setTime(date);
	     int maxDate=cal.getActualMaximum(Calendar.DATE);
		 // int minDate=cal.getActualMinimum(5);
		 cal.set(Calendar.DATE, maxDate);
		 Date date2 = cal.getTime();
		 return date2;		
	}
	
	/**
	 * 获取现在小时
	 */
	public static String getHour(){
		  Date currentTime = new Date();
		  SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		  String dateString = formatter.format(currentTime);
		  String hour;
		  hour = dateString.substring(11, 13);
		  //substring(m,n) 包括m不包括n,正数
		  //substr(m,length) m可以是负数,指从尾部开始算
		  //slice(m,n) m,n可负
		  return hour;
	}
	
	/**
	 * 根据用户传入的时间表示格式,返回当前时间的格式 如果是yyyyMMdd,注意字母y不能大写。
	 * yyyyMMddhhmmss
	 */
	public static String getUserDate(String sformat) {
		  Date currentTime = new Date();
		  SimpleDateFormat formatter = new SimpleDateFormat(sformat);
		  String dateString = formatter.format(currentTime);
		  return dateString;
		  }
	
	/**
	 * 计算时间差
	 * 传入格式为 yyyy-MM-dd HH:mm:ss
	 */
	public static void timeDiff(String startTime,String endTime){
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");   
        long nd = 1000 * 24 * 60 * 60;// 一天的毫秒数     
        long nh = 1000 * 60 * 60;// 一小时的毫秒数     
        long nm = 1000 * 60;// 一分钟的毫秒数     
        long ns = 1000;// 一秒钟的毫秒数     
        long diff;     
        long day = 0;     
        long hour = 0;     
        long min = 0;     
        long sec = 0;     
        // 获得两个时间的毫秒时间差异
        try{
            diff = sdf.parse(endTime).getTime() - sdf.parse(startTime).getTime();     
            day = diff / nd;// 计算差多少天     
            hour = diff % nd / nh + day * 24;// 计算差多少小时     
            min = diff % nd % nh / nm + day * 24 * 60;// 计算差多少分钟     
            sec = diff % nd % nh % nm / ns;// 计算差多少秒     
            // 输出结果     
            System.out.println("时间相差:" + day + "天" + (hour - day * 24) + "小时"    
                    + (min - day * 24 * 60) + "分钟" + sec + "秒。");     
            System.out.println("hour=" + hour + ",min=" + min); 
        }catch(ParseException e){
        e.printStackTrace();
        }
	}
	
	/**
	 * 时间前推或者后推  JJ表示分钟
	 */
	public static String getPredTime(String time1,String JJ){
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String time2=" ";
		try {
			Date date1=sdf.parse(time1);
			long time=(date1.getTime()/1000) + Integer.parseInt(JJ)*60;
			date1.setTime(time*1000);
			time2=sdf.format(date1);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return time2;		
	}
	
	/*
	 * 判断是否为闰年
     * 详细设计: 1.被400整除是闰年,否则: 2.不能被4整除则不是闰年 3.能被4整除同时不能被100整除则是闰年
     * 3.能被4整除同时能被100整除则不是闰年
	 */
	public static boolean isLeapYear(String dateString){
		SimpleDateFormat sdf =new SimpleDateFormat("yyyy-MM-dd");
		ParsePosition pos=new ParsePosition(0);
		Date date=sdf.parse(dateString,pos);
		GregorianCalendar gc=(GregorianCalendar) Calendar.getInstance();
		gc.setTime(date);
		int year=gc.get(GregorianCalendar .YEAR);
		if(year%400==0)
			return true;
		else if(year%4==0) {
			if(year%100==0)
				return false;
			else {
				return true;
			}
		}
		return false;
	}
	
	/*
	 * 返回美国格式 11 Apr 2012
	 */
	public static String getUSTime(String str){
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
		ParsePosition pos= new ParsePosition(0);
		Date USTime=sdf.parse(str, pos);
		String str2=USTime.toString();
		String[] str3=str2.split(" ");
		return str3[2]+str3[1].toUpperCase()+str3[5];
	}
	
	/*
	 * 判断两个时间是否在同一周
	 */
	public static boolean isSameWeek(Date date1,Date date2){
		Calendar cal1=Calendar.getInstance();
		Calendar cal2=Calendar.getInstance();
		cal1.setTime(date1);
		cal2.setTime(date2);
		int subYear=cal1.get(Calendar.YEAR)-cal2.get(Calendar.YEAR);
		if(subYear==0)
			if(cal1.get(Calendar.WEEK_OF_YEAR)==cal2.get(Calendar.WEEK_OF_YEAR))
				return true;
			else if(subYear==1&&11==cal2.get(Calendar.MONTH)){
				if(cal1.get(Calendar.WEEK_OF_YEAR)==cal2.get(Calendar.WEEK_OF_YEAR))
					return true;
			}else if(subYear==-1&&11==cal1.get(Calendar.MONTH)){
				if(cal1.get(Calendar.WEEK_OF_YEAR)==cal2.get(Calendar.WEEK_OF_YEAR))
					return true;
			}
		return false;							
	}
	
	/*
	 * 产生周序列,即得到当前时间所在的年度是第几周
	 */
	public static String getSeqWeek(){
		Calendar cal=Calendar.getInstance(Locale.CHINA);
		String week = Integer.toString(cal.get(Calendar.WEEK_OF_YEAR));
		if(week.length()==1)
			week="0"+week;
		String year=Integer.toString(cal.get(Calendar.YEAR));
		return year+" "+week;
	}
	
	/*
	 * 获得一个日期所在的周的星期几的日期,如要找出2002年2月3日所在周的星期一是几号
	 */
	public static String getWeekDate(String getDate,String num){
		SimpleDateFormat sdf =new SimpleDateFormat("yyyy-MM-dd");
		ParsePosition pos=new ParsePosition(0);
		Date date=sdf.parse(getDate,pos);
		GregorianCalendar gc=(GregorianCalendar) Calendar.getInstance();
		gc.setTime(date);
		if(num.equals("1"))
			gc.set(Calendar.DAY_OF_WEEK,Calendar.MONDAY);
		if(num.equals("2"))
			gc.set(Calendar.DAY_OF_WEEK, Calendar.TUESDAY);
		if(num.equals("3"))
			gc.set(Calendar.DAY_OF_WEEK, Calendar.WEDNESDAY);
		if(num.equals("4"))
			gc.set(Calendar.DAY_OF_WEEK, Calendar.THURSDAY);
		if(num.equals("5"))
			gc.set(Calendar.DAY_OF_WEEK, Calendar.FRIDAY);
		if(num.equals("6"))
			gc.set(Calendar.DAY_OF_WEEK, Calendar.SATURDAY);
		if(num.equals("7"))
			gc.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
		return new SimpleDateFormat("yyyy-MM-dd").format(gc.getTime());		
	}
	
	/*
	 * 计算两个时间差
	 */
	public static long timeDiffer(String time1,String time2){
		if(time1==null||time1.equals(0))
			return 0;
		if(time2==null||time2.equals(0))
			return 0;
		
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Date date1=null;
		Date date2=null;
		try{
			date1=sdf.parse(time1);
			date2=sdf.parse(time2);
		}catch(Exception e){		
		}
		long day=(date1.getTime()-date2.getTime())/(24*60*60*1000);
		return day;
	}
	
	/*
     * 形成如下的日历 , 根据传入的一个时间返回一个结构 星期日 星期一 星期二 星期三 星期四 星期五 星期六 下面是当月的各个时间
     * 此函数返回该日历第一行星期日所在的日期
	 */
	public static String getSunOfFirstWeek(String getDate){
		getDate=getDate.substring(0,8)+"01";
		SimpleDateFormat sdf =new SimpleDateFormat("yyyy-MM-dd");
		ParsePosition pos=new ParsePosition(0);
		Date date=sdf.parse(getDate,pos);
		GregorianCalendar gc=(GregorianCalendar) Calendar.getInstance();
		gc.setTime(date);
		int u =gc.get(Calendar.DAY_OF_WEEK);
		long sunTime=date.getTime()/1000+(1-u)*24*60*60;
		date.setTime(sunTime*1000);
		String sundayTime=sdf.format(date);
		return sundayTime;		
	}
}


有Date,Calendar,String表示及转换,还有一些库里头的东东 -。=


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值