android时间转换成时间戳
❶ android 时间滚轮怎么把选择的每个选项合并后转换成时间戳
1、添加compile:(这个库里用到了fastjson)
compile 'cn.qqtheme.framework:WheelPicker:1.2.3'
compile 'com.alibaba:fastjson:1.2.18'1212
2、assets文件夹下添加city.json,如下图:
3、代码中使用:
/**
* 地区选择器
*/
private void showRegionPicker() {
try {
final ArrayList<AddressPicker.Province> data = new ArrayList<>();
String json = ConvertUtils.toString(getAssets().open("city.json"));
data.addAll(JSON.parseArray(json, AddressPicker.Province.class));
final AddressPicker picker
❷ 微信小程序——时间戳与Date对象的那些事
做过web开发的小伙伴们多少会遇到时间转换问题在Chrome浏览器和Safari浏览器的不同,报错或者时间格式等等,这里我就分享一下我在开发过程中遇到的一些问题
关于时间问题,做过日历的都知道,全球化时区处理,或者时间格式,或者时间戳转化,这里我就这几种情况一一讨论
由于微信小程序的部分业务需要预定,所以会出现国外预定到国内时间会有偏差的问题,根本原因还是在于new Date(),它会获取当前系统时间,大部分手机是设置的是自动时间,所以会出现时区的偏差问题。
思考 :如果产品是某地的,那么预定时间应该为对应地的时区计算。所以需要维护定位与时区之间的关系
关于时区计算,因为我这边只用到了北京时间的情况,举个例子:
Safari浏览器中,进行 new Date("yyyy-MM-dd hh:mm:ss") 的时候,会出现报错Invalid Date,或者转换出问题的情况,原因是Safari并不支持此格式,所以需要调整格式化的方式。
ECMA-262 标准中( Date Time String Format )将日期格式规定为 YYYY-MM-DDTHH:mm:ss.sssZ ,其中, T 标识时间开始, Z 为相对于UTC(协调世界时 - International Atomic Time)的时间偏移量,可为 Z , +HH:mm 或 -HH:mm 。
思考 :要么正则替换为标准的 / 分割,要么在中间空白处加上 T ,看自己喜好
看文档是支持直接毫秒数时间戳转Date对象的,正常操作也是如此。
PS :但是在微信小程序的iOS端转过来的实际时间是UTC的时间,所以需要同步为当前时区时间,为了不影响Android的时间结果,所以通过转换2次的方式解决时间计算问题。
ECMA-262 标准来源参考: https://www.jianshu.com/p/a11196377048
❸ android开发,long型时间怎么取出对应的年月日
long类型的时间说明获取得到的是时间戳,具体转换可参考以下代码
java">//mill为你龙类型的时间戳
Datedate=newDate(mill);
Stringstrs="";
try{
//yyyy表示年MM表示月dd表示日
//yyyy-MM-dd是日期的格式,比如2015-12-12如果你要得到2015年12月12日就换成yyyy年MM月dd日
SimpleDateFormatsdf=newSimpleDateFormat("yyyy-MM-dd");
//进行格式化
strs=sdf.format(date);
System.out.println(strs);
}catch(Exceptione){
e.printStackTrace();
}
❹ 如何用Android写一个时间戳编码程序‘
这是我项目中正在用的时间戳,没经过整理,你看下
package com.tianwei.utils;
import android.net.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
/**
* Created by GT on 2017/8/22.
* 注:Uinix和Windows时间不同
*/
public class Time {
public void Time() {
}
//格式时间
public static String systemTime(String time) {
SimpleDateFormat sDateFormat = null;
if (time != null && time.length() > 0) {
sDateFormat = new SimpleDateFormat(time);
} else {
sDateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
}
String date = sDateFormat.format(new java.util.Date());
return date;
}
//无格式时间
public static String systemTime() {
SimpleDateFormat sDateFormat = null;
sDateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
String date = sDateFormat.format(new java.util.Date());
return date;
}
/*
* 将时间戳转换为时间
*/
public static String stampToDate(String s, String time) {
String res;
SimpleDateFormat simpleDateFormat;
if (time == null && time.length() > 0) {
simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
} else {
simpleDateFormat = new SimpleDateFormat(time);
}
long lt = new Long(s);
Date date = new Date(lt);
res = simpleDateFormat.format(date);
return res;
}
/*
* 将时间转换为时间戳
*/
public static String dateToStamp(String s) throws ParseException {
String res;
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = null;
try {
date = simpleDateFormat.parse(s);
} catch (java.text.ParseException e) {
e.printStackTrace();
}
long ts = date.getTime();
res = String.valueOf(ts);
return res;
}
/**
* 系统时间戳
*/
public static long dataStampDate() {
long s = System.currentTimeMillis();
// long s = new Date().getTime();
// long s = Calendar.getInstance().getTimeInMillis();
return s;
}
/**
* Unix
* 时间戳转换成日期格式
*
* @param timestampString
* @param formats
* @return
*/
public static String timeStampUnixDate(String timestampString, String formats) {
Long timestamp = Long.parseLong(timestampString) * 1000;
String date = new java.text.SimpleDateFormat(formats).format(new java.util.Date(timestamp));
return date;
}
/**
* Unix
* 日期格式字符串转换成时间戳
*
* @param dateStr 字符串日期
* @param format 如:yyyy-MM-dd HH:mm:ss
* @return
*/
public static String dateUinxTimeStamp(String dateStr, String format) {
try {
SimpleDateFormat sdf = null;
if (format != null && format.length() > 0) {
sdf = new SimpleDateFormat(format);
} else {
sdf = new SimpleDateFormat("yyyyMMddhhmmss");
}
return String.valueOf(sdf.parse(dateStr).getTime() / 1000);
} catch (Exception e) {
e.printStackTrace();
}
return "";
}
/**
* 两个时间间的时间戳计算函数
*
* @param beginDate
* @param endDate
* @param f 时间差的形式0:秒,1:分种,2:小时,3:天
* @return long 秒
*/
public static long getDifference(Date beginDate, Date endDate, int f) {
long result = 0;
if (beginDate == null || endDate == null) {
return 0;
}
try {
// 日期相减获取日期差X(单位:毫秒)
long millisecond = endDate.getTime() - beginDate.getTime();
/**
* Math.abs((int)(millisecond/1000)); 绝对值 1秒 = 1000毫秒
* millisecond/1000 --> 秒 millisecond/1000*60 - > 分钟
* millisecond/(1000*60*60) -- > 小时 millisecond/(1000*60*60*24) -->
* 天
* */
switch (f) {
case 0: // second
return (millisecond / 1000);
case 1: // minute
return (millisecond / (1000 * 60));
case 2: // hour
return (millisecond / (1000 * 60 * 60));
case 3: // day
return (millisecond / (1000 * 60 * 60 * 24));
}
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
/**
* 计算时间差
*
* @param starTime 开始时间
* @param endTime 结束时间
* @return 返回时间差
* @param返回类型==1----天,时,分。 ==2----时
*/
public String getTimeDifference(String starTime, String endTime) {
String timeString = "";
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
try {
Date parse = dateFormat.parse(starTime);
Date parse1 = dateFormat.parse(endTime);
long diff = parse1.getTime() - parse.getTime();
long day = diff / (24 * 60 * 60 * 1000);
long hour = (diff / (60 * 60 * 1000) - day * 24);
long min = ((diff / (60 * 1000)) - day * 24 * 60 - hour * 60);
long s = (diff / 1000 - day * 24 * 60 * 60 - hour * 60 * 60 - min * 60);
long ms = (diff - day * 24 * 60 * 60 * 1000 - hour * 60 * 60 * 1000
- min * 60 * 1000 - s * 1000);
// System.out.println(day + "天" + hour + "小时" + min + "分" + s +
// "秒");
long hour1 = diff / (60 * 60 * 1000);
String hourString = hour1 + "";
long min1 = ((diff / (60 * 1000)) - hour1 * 60);
timeString = hour1 + "小时" + min1 + "分";
// System.out.println(day + "天" + hour + "小时" + min + "分" + s +
// "秒");
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (java.text.ParseException e) {
e.printStackTrace();
}
return timeString;
}
/**
* Java YEAR、MONTH、DAY_OF_MONTH、HOUR加减法,int num +(日期前) -(日期后)
*
* @param num
* @param type
* @return
*/
public static String timeDateCompute(int num, int type) {
// YEAR、MONTH、DAY_OF_MONTH、HOUR 等
Calendar cal = Calendar.getInstance();//使用默认时区和语言环境获得一个日历。
if (type > 6) {
return null;
}
switch (type) {
case 0://年
cal.add(Calendar.YEAR, -num);
break;
case 1://月
cal.add(Calendar.MONTH, -num);
break;
case 2://日
cal.add(Calendar.DAY_OF_MONTH, -num);//取当前日期的前num天.
break;
case 3://时
cal.add(Calendar.HOUR_OF_DAY, -num);
break;
case 4://分
cal.add(Calendar.MINUTE, -num);
break;
case 5://秒
cal.add(Calendar.SECOND, -num);
break;
case 6://周
cal.add(Calendar.WEEK_OF_MONTH, -num);
break;
}
//通过格式化输出日期
SimpleDateFormat format = new java.text.SimpleDateFormat("yyyyMMddHHmmss");
// SimpleDateFormat format = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
if (type == 0) {
System.out.println("Today is: " + format.format(Calendar.getInstance().getTime()));
}
System.out.println("CutNum is: " + format.format(cal.getTime()));
String CutNum = format.format(cal.getTime());
return CutNum;
}
/**
* 时间日期加减(-前,+后)
*
* @param statTime
* @param ymdhms
* @param type
* @return
*/
public String timeNum(Date statTime, int ymdhms, int type) {
String tn = null;
SimpleDateFormat df = new SimpleDateFormat("yyyyMMddhhmmss");
System.out.println("今天的日期:" + df.format(statTime));
System.out.println("两天前的日期:" + df.format(new Date(statTime.getTime() - 2 * 24 * 60 * 60 * 1000)));
System.out.println("三天后的日期:" + df.format(new Date(statTime.getTime() + 3 * 24 * 60 * 60 * 1000)));
switch (type) {
case 0://年
break;
case 1://月
break;
case 2://日
tn = df.format(new Date(statTime.getTime() - ymdhms * 24 * 60 * 60 * 1000));
break;
case 3://时
tn = df.format(new Date(statTime.getTime() - ymdhms * 60 * 60 * 1000));
break;
case 4://分
tn = df.format(new Date(statTime.getTime() - ymdhms * 60 * 1000));
break;
case 5://秒
tn = df.format(new Date(statTime.getTime() - ymdhms * 1000));
break;
}
return tn;
}
/**
* 时间日期加减(-前,+后)
*
* @param statTime
* @param year
* @param month
* @param day
* @param hour
* @param min
* @param sec
* @return
*/
public String timeNumStr(Date statTime, int year, int month, int day, int hour, int min, int sec) {
String tn = null;
SimpleDateFormat df = new SimpleDateFormat("yyyyMMddhhmmss");
System.out.println("今天的日期:" + df.format(statTime));
System.out.println("两天前的日期:" + df.format(new Date(statTime.getTime() - 2 * 24 * 60 * 60 * 1000)));
System.out.println("三天后的日期:" + df.format(new Date(statTime.getTime() + 3 * 24 * 60 * 60 * 1000)));
tn = df.format(new Date(statTime.getTime() - day * hour * min * sec * 1000));
return tn;
}
}
❺ 怎么把指定时间转为时间戳
每种语言转换成时间戳的方法都不一样,你可以去码工具网上搜时间戳转换,里面有各个语言时间戳转换的方法。
❻ 求助,怎么将时间转成时间戳格式
将【北京标准时间】转换成【格林威治的标准时间】
.参数 要转换的北京时间, 日期时间型, , 如: 1970-01-01 08:00:00 或 2009年2月9日12时36分36秒
.参数 是否为秒, 逻辑型, 可空 , 设置返回的时间戳记值单位 真:秒 假:毫秒 默认为真
.参数 是否为北京时间, 逻辑型, 可空 , 默认为真
精易模块的 命令
❼ Android 怎么获取当前的时间戳
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;
}
❽ 如何将android时间戳转换成时间 android怎么把时间戳转换成小时
mysql数据库的日期字段类型建议为varchar或者char,存入时间戳。 取出的时候,将时间戳转换为你需要的时间格式就好。 例: 假设取出值为$time echo date('Y-m-d H:i:s',$time); 你就会看到:2011-11-23 17:42:43的时间格式
❾ android 将时间戳转为代表"距现在多久之前"的字符串
/**
* 将时间戳转为代表"距现在多久之前"的字符串
* @param timeStr 时间戳
* @return
*/
public static String getStandardDate(String timeStr) {
StringBuffer sb = new StringBuffer();
long t = Long.parseLong(timeStr);
long time = System.currentTimeMillis() - (t*1000);
long mill = (long) Math.ceil(time /1000);//秒前
long minute = (long) Math.ceil(time/60/1000.0f);// 分钟前
long hour = (long) Math.ceil(time/60/60/1000.0f);// 小时
long day = (long) Math.ceil(time/24/60/60/1000.0f);// 天前
if (day - 1 > 0) {
sb.append(day + "天");
} else if (hour - 1 > 0) {
if (hour >= 24) {
sb.append("1天");
} else {
sb.append(hour + "小时");
}
} else if (minute - 1 > 0) {
if (minute == 60) {
sb.append("1小时");
} else {
sb.append(minute + "分钟");
}
} else if (mill - 1 > 0) {
if (mill == 60) {
sb.append("1分钟");
} else {
sb.append(mill + "秒");
}
} else {
sb.append("刚刚");
}
if (!sb.toString().equals("刚刚")) {
sb.append("前");
}
return sb.toString();
}