Android 怎么获取当前的时间戳
1个回答
2015-11-26 · 做真实的自己 用良心做教育
千锋教育
千锋教育专注HTML5大前端、JavaEE、Python、人工智能、UI&UE、云计算、全栈软件测试、大数据、物联网+嵌入式、Unity游戏开发、网络安全、互联网营销、Go语言等培训教育。
向TA提问
关注
展开全部
Android获取当前时间代码
//需要引用的
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
//详细代码
java.util.Date currentdate = new java.util.Date();//当前时间
//long i = (currentdate.getTime()/1000-timestamp)/(60);
//System.out.println(currentdate.getTime());
//System.out.println(i);
Timestamp now = new Timestamp(System.currentTimeMillis());//获取系统当前时间
System.out.println("now-->"+now);//返回结果精确到毫秒。
时间戳转日期
int timestamp = 1310457552; //将这个时间戳转为日期
return getTime(timestamp);
定义getTime, getDate, IntToLong
public static String getTime(int timestamp){
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String time= null;
try {
String str = sdf.format(new Timestamp(IntToLong(timestamp)));
time = str.substring(11, 16);
String month = str.substring(5, 7);
String day = str.substring(8,10 );
time =getDate(month, day)+ time;
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return time;
}
public static String getDate(String month,String day){
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//24小时制
java.util.Date d = new java.util.Date(); ;
String str = sdf.format(d);
String nowmonth = str.substring(5, 7);
String nowday = str.substring(8,10 );
String result = null;
int temp = Integer.parseInt(nowday)-Integer.parseInt(day);
switch (temp) {
case 0:
result="今天";
break;
case 1:
result = "昨天";
break;
case 2:
result = "前天";
break;
default:
StringBuilder sb = new StringBuilder();
sb.append(Integer.parseInt(month)+"月");
sb.append(Integer.parseInt(day)+"日");
result = sb.toString();
break;
}
return result;
}
//java Timestamp构造函数需传入Long型
public static long IntToLong(int i){
long result = (long)i;
result*=1000;
return result;
}
//需要引用的
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
//详细代码
java.util.Date currentdate = new java.util.Date();//当前时间
//long i = (currentdate.getTime()/1000-timestamp)/(60);
//System.out.println(currentdate.getTime());
//System.out.println(i);
Timestamp now = new Timestamp(System.currentTimeMillis());//获取系统当前时间
System.out.println("now-->"+now);//返回结果精确到毫秒。
时间戳转日期
int timestamp = 1310457552; //将这个时间戳转为日期
return getTime(timestamp);
定义getTime, getDate, IntToLong
public static String getTime(int timestamp){
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String time= null;
try {
String str = sdf.format(new Timestamp(IntToLong(timestamp)));
time = str.substring(11, 16);
String month = str.substring(5, 7);
String day = str.substring(8,10 );
time =getDate(month, day)+ time;
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return time;
}
public static String getDate(String month,String day){
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//24小时制
java.util.Date d = new java.util.Date(); ;
String str = sdf.format(d);
String nowmonth = str.substring(5, 7);
String nowday = str.substring(8,10 );
String result = null;
int temp = Integer.parseInt(nowday)-Integer.parseInt(day);
switch (temp) {
case 0:
result="今天";
break;
case 1:
result = "昨天";
break;
case 2:
result = "前天";
break;
default:
StringBuilder sb = new StringBuilder();
sb.append(Integer.parseInt(month)+"月");
sb.append(Integer.parseInt(day)+"日");
result = sb.toString();
break;
}
return result;
}
//java Timestamp构造函数需传入Long型
public static long IntToLong(int i){
long result = (long)i;
result*=1000;
return result;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询