java将时间戳转为字符串 将字符串转为时间戳

48 篇文章 0 订阅
 /**
* @Description 获取当前时间 
* @author YuSy
* @return
*/
public static String getNowDateTime() {
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-M-d HH:mm");
return sdf.format(date);  //当期日期

}


SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式

df.format(new Date())




/**
* @Description 是否大于现在的时间
* @author YuSy
* @param time "yyyy-M-d HH:mm"时间格式
* @return
*/
public static boolean isGreaterNowTime(String time){
if (isNull(time)) {
return false;
}
long nowTime=System.currentTimeMillis();
long theTime=Long.valueOf(getTime(time,"yyyy-M-d HH:mm"));
if (theTime>nowTime) {
return true;
}else{
return false;
}
}



public static String getTheTime(String cc_time) {
if (isNull(cc_time)) {
return "";
}
long lcc_time = Long.parseLong(cc_time);
SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy/MM/dd");
return sdf2.format(lcc_time);

}






/**
* 将字符串转为时间戳
* @param user_time  例如:user_time = "2015-1-3 09:03"; 
* @param format  格式  例如: "yyyy-M-d HH:mm"; 
* @return
*/
public static String getTime(String user_time,String format) {
SimpleDateFormat sdf = new SimpleDateFormat(format);
Date d;
if (isNull(user_time)) {
return "";
}
try {
d = sdf.parse(user_time);
long l = d.getTime();
return String.valueOf(l);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return "";
}





/**
* java将时间戳转为字符串

* @param cc_time  例如:cc_time="1291778220"
* @param format  格式  例如: "yyyy-MM-dd HH:mm:ss"; 
* @return 返回此形式的数据yyyy-MM-dd HH:mm:ss
*/
public static String getStrTime2(String cc_time,String format) {
long lcc_time = Long.parseLong(cc_time);
return getStrTime2(lcc_time, format);
}
/**
* java将时间戳转为字符串

* @param cc_time  例如:cc_time=1291778220
* @param format  格式  例如: "yyyy-MM-dd HH:mm:ss"; 
* @return 返回此形式的数据yyyy-MM-dd HH:mm:ss
*/
public static String getStrTime2(long cc_time,String format) {
SimpleDateFormat sdf2 = new SimpleDateFormat(format);
return sdf2.format(cc_time);
}



  String timeBefore=OtherUtil.getTime(time, "yyyy-MM-dd HH:mm:ss");
 String timeAfter=OtherUtil.getStrTime2(timeBefore, "yy-MM-dd HH:mm");


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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值