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

来源:互联网 发布:mma知乎 编辑:程序博客网 时间:2024/05/20 18:49
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