java时间处理,时间格式转换

package yaSoft.Util;

import java.sql.Time;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

public class TimeUtil {
	
	/**
	 * 转换成Date形式
	 * @param Time
	 * @return
	 */
	public static Date getTime(String Time){
		SimpleDateFormat sdf =new SimpleDateFormat("yyyy-MM-dd");
		Date date = new Date();
		try {  
	            date = sdf.parse(Time);
	        } catch (Exception e) {  
	            e.printStackTrace();  
	        }  
		return date;
	}
	/**
	 * 转换成datetime形式
	 * @param operatingTime
	 * @return
	 */
	public static Timestamp getOperateTime(String operatingTime){
		Timestamp ts = new Timestamp(System.currentTimeMillis()); 
		 try {  
	            ts = Timestamp.valueOf(operatingTime);  
	            System.out.println(ts);  
	        } catch (Exception e) {  
	            e.printStackTrace();  
	        }  
		return ts;
	}
	//datetime转换成string形式

	public static String getDateTime(Timestamp operatingTime){
		SimpleDateFormat sdf =new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String time = "";
		try {  
	            //方法一  
			    time = sdf.format(operatingTime);  
	            System.out.println(time);  
	          /*  //方法二  
	            time = ts.toString();  
	            System.out.println(time); */ 
	        } catch (Exception e) {  
	            e.printStackTrace();  
	        }  
		return time;
	}
	
	/**
	 * datetime转换成string形式 格式为yyyy-MM-dd
	 * @param operatingTime
	 * @return
	 */

		public static String getDate(Timestamp operatingTime){
			SimpleDateFormat sdf =new SimpleDateFormat("yyyy-MM-dd");
			String time = "";
			try {  
		            //方法一  
				    time = sdf.format(operatingTime);  
		            System.out.println(time);  
		          /*  //方法二  
		            time = ts.toString();  
		            System.out.println(time); */ 
		        } catch (Exception e) {  
		            e.printStackTrace();  
		        }  
			return time;
		}
	/**
	 * 获取系统时间转换成datetime类型
	 * @return
	 */
	public static Timestamp getSysTime(){
		Date date=new Date();
		SimpleDateFormat dateFormat =new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String currTime=dateFormat.format(date);
		Timestamp ts = new Timestamp(System.currentTimeMillis()); 
		 try {  
	            ts = Timestamp.valueOf(currTime);  
	            System.out.println(ts);  
	        } catch (Exception e) {  
	            e.printStackTrace();  
	        }  
		return ts;
	}
	/**
	 * 获取系统时间转换成datetime类型
	 * @return
	 */
	public static Time getSysTimes(){
		Date date=new Date();
		SimpleDateFormat dateFormat =new SimpleDateFormat("HH:mm:ss");
		String currTime=dateFormat.format(date);
		Time ts = new Time(System.currentTimeMillis()); 
		 try {  
	            ts = Time.valueOf(currTime);  
	            System.out.println(ts);  
	        } catch (Exception e) {  
	            e.printStackTrace();  
	        }  
		return ts;
	}
	/**
	 * 获取系统时间转换成date类型
	 * @return
	 */
	public static Date getSysDate(){
		Date date=new Date();
		SimpleDateFormat sdf =new SimpleDateFormat("yyyy-MM-dd");
		String currTime=sdf.format(date);
		 try {  
			 date = sdf.parse(currTime);  
	        } catch (Exception e) {  
	            e.printStackTrace();  
	        }  
		return date;
	}
	public static String getTime(){
		Date date=new Date();
		SimpleDateFormat dateFormat =new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
		String currTime=dateFormat.format(date);
		return currTime;
	}
	public static String getYear(){
		Date date=new Date();
		SimpleDateFormat dateFormat =new SimpleDateFormat("yyyy");
		String currTime=dateFormat.format(date);
		return currTime;
	}
	public static String getOrderTime(){
		Date date=new Date();
		SimpleDateFormat dateFormat =new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String currTime=dateFormat.format(date);
		return currTime;
	}
	public static String getPayTime(){
		Date date=new Date();
		SimpleDateFormat dateFormat =new SimpleDateFormat("yyyyMMddhhmmss");
		String currTime=dateFormat.format(date);
		return currTime;
	}
	public static String getPayTime2(){
		Date date=new Date();
		SimpleDateFormat dateFormat =new SimpleDateFormat("yyyy/MM/dd");
		String currTime=dateFormat.format(date);
		return currTime;
	}
	public static String getTimes(Date time){
		SimpleDateFormat dateFormat =new SimpleDateFormat("yyyy-MM-dd");
		String currTime=dateFormat.format(time);
		return currTime;
	}
	public static String getDateTimes(Date time){
		SimpleDateFormat dateFormat =new SimpleDateFormat("yyyy-MM-dd");
		String currTime=dateFormat.format(time);
		return currTime;
	}
	public static String getDateTimes2(Date time){
		SimpleDateFormat dateFormat =new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String currTime=dateFormat.format(time);
		return currTime;
	}
	public static void main(String[] args) {
		String i ="2,3,";
		System.out.println(i.substring(i.length()-1,i.length()).equals(","));
		System.out.println();
	}
	public static String getImagesTime(){
		Date date=new Date();
		SimpleDateFormat dateFormat =new SimpleDateFormat("yyyyMMddHHmmss");
		String currTime=dateFormat.format(date);
		return currTime;
	}
	public static String getWeixinTime(){
		Date date=new Date();
		SimpleDateFormat dateFormat =new SimpleDateFormat("MM月dd日");
		String currTime=dateFormat.format(date);
		return currTime;
	}
	public static String getDay(){
		Date date=new Date();
		SimpleDateFormat dateFormat =new SimpleDateFormat("dd");
		String currTime=dateFormat.format(date);
		return currTime;
	}
	/**
	 *格林时间转换成字符串
	 *@author lmc
	 *@time 2014-12-11 17:29:11 
	 */
	public static String greenwichMeanTime(String timeStr){
	    Date d = null;
        SimpleDateFormat sdfSource = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy", Locale.US);
        SimpleDateFormat sdfTarget = new SimpleDateFormat("yyyy-MM-dd", Locale.US);//设置显示格式
        try {
            d = sdfSource.parse(timeStr);
        } catch (ParseException e) {
            return null;
        }
        return sdfTarget.format(d);
	}
	
	/**
	 * 格林时间转转换成date类型
	 * @return
	 */
	public static Date greenwichMeanTimes(String timeStr){
		Date date=new Date();
		SimpleDateFormat sdf =new SimpleDateFormat("yyyy-MM-dd");
		 try {  
			 date = sdf.parse(timeStr);  
	        } catch (Exception e) {  
	            e.printStackTrace();  
	        }  
		return date;
	}
    /**
     * 把毫秒转化成日期
     * @param dateFormat(日期格式,例如:MM/ dd/yyyy HH:mm:ss)
     * @param millSec(毫秒数)
     * @return
     */
    @SuppressWarnings("unused")
	public static String transferLongToDate(String dateFormat,Long millSec){
     SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
     Date date= new Date(millSec);
     return sdf.format(date);
    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值