Android:Date、String、Long三种日期类型之间的相互转换
2016-04-01 22:00
639 查看
date类型转换为String类型
// formatType格式为yyyy-MM-dd HH:mm:ss//yyyy年MM月dd日 HH时mm分ss秒 // data Date类型的时间 public static String dateToString(Date data, String formatType) { return new SimpleDateFormat(formatType).format(data); }
long类型转换为String类型
// currentTime要转换的long类型的时间 // formatType要转换的string类型的时间格式 public static String longToString(long currentTime, String formatType) throws ParseException { Date date = longToDate(currentTime, formatType); // long类型转成Date类型 String strTime = dateToString(date, formatType); // date类型转成String return strTime; }
string类型转换为date类型
// strTime要转换的string类型的时间,formatType要转换的格式yyyy-MM-dd HH:mm:ss//yyyy年MM月dd日 // HH时mm分ss秒, // strTime的时间格式必须要与formatType的时间格式相同 public static Date stringToDate(String strTime, String formatType) throws ParseException { SimpleDateFormat formatter = new SimpleDateFormat(formatType); Date date = null; date = formatter.parse(strTime); return date; }
long转换为Date类型
// currentTime要转换的long类型的时间 // formatType要转换的时间格式yyyy-MM-dd HH:mm:ss//yyyy年MM月dd日 HH时mm分ss秒 public static Date longToDate(long currentTime, String formatType) throws ParseException { Date dateOld = new Date(currentTime); // 根据long类型的毫秒数生命一个date类型的时间 String sDateTime = dateToString(dateOld, formatType); // 把date类型的时间转换为string Date date = stringToDate(sDateTime, formatType); // 把String类型转换为Date类型 return date; }
String类型转换为long类型
// strTime要转换的String类型的时间 // formatType时间格式 // strTime的时间格式和formatType的时间格式必须相同 public static long stringToLong(String strTime, String formatType) throws ParseException { Date date = stringToDate(strTime, formatType); // String类型转成date类型 if (date == null) { return 0; } else { long currentTime = dateToLong(date); // date类型转成long类型 return currentTime; } }
date类型转换为long类型
// date要转换的date类型的时间 public static long dateToLong(Date date) { return date.getTime(); }
相关文章推荐
- 开发Flex for Android第一个ANE(ActionScript Native Extensions)本地扩展
- Android Studio 配置NDK,运行第一个hello-jni安卓程序详细步骤
- Android软键盘弹出时把布局顶上去的解决方法
- Android之NDK开发
- Android中涉及到的焦点问题,focusable,clickable,enabled
- Android如何保证一个线程最多只能有一个Looper?
- 百度地图开发(三):Android studio添加jar包和so库
- Android ORM 数据库的使用
- Android从图库选择照片
- Android Studio打包Android Library为jar方案
- android intent MIME type
- Android杂记(2):RecyclerView中Button点击问题
- Android MTK 6572 修改开机logo和开机动画
- Android Intent
- Android官方说明--返回键和toolbar向上按钮的区别
- VisionMobile:2015年Android使用的全球趋势(六)巴西喜欢WI-FI,印度喜欢蜂窝
- Android MTK Launcher3 替换桌面图标
- 《Pro Android Graphics》读书笔记之第四节
- Android Studio 常用快捷键
- Android上实现仿IOS弹性ScrollView