java时间处理,时间格式转换
2014-12-13 15:15
337 查看
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); } }
相关文章推荐
- Util:java代码中时间格式转换及表单防重复提交处理
- Java的时间处理(续)
- (转载)java语言对时间的处理
- java中对系统当前时间的处理
- Java日期时间处理的简要使用方法
- 浅谈JAVA时间日期处理函数
- Java的时间处理(续)
- Java处理时间格式化
- JAVA时间格式化处理
- Java时间格式转换
- Java的时间处理(续)
- struts时间格式转换问题处理方案
- java中的日期和时间处理示例
- Java的时间处理(续)
- JAVA 处理时间 - java.sql.Date、java.util.Date与数据库中的Date字段的转换方法
- (转载)java语言对时间的处理
- Java的时间处理(续<计算Java时间>)
- java语言对时间的处理
- JAVA 处理时间 - java.sql.Date、java.util.Date与数据库中的Date字段的转换方法
- Java中对时间、日期的处理