• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
The Path of Growth
All Programers!
博客园    首页    新随笔    联系   管理    订阅  订阅
java中获得时间及个别操作


// 博客搬家咯~ http://joeleee.github.io/


// 博客搬家咯~ http://joeleee.github.io/


// 博客搬家咯~ http://joeleee.github.io/




public
static void main(String[] args) {

SimpleDateFormat matter = new SimpleDateFormat("yyyy-MM-dd_HH:mm"); // 自定义时间格式

Calendar calendar = Calendar.getInstance(); // 获得当前时间
Date date1 = calendar.getTime(); // 将calendar的时间赋值给date1
calendar.add(Calendar.MINUTE, -100); // 将calendar的时间减去100分钟
Date date2 = calendar.getTime(); // 将calendar的时间赋值给date2
Date date3 = null;

try {
date3 = matter.parse("1989-05-18_10:21"); // 按照自定义的格式自定时间
} catch (ParseException e) {
e.printStackTrace();
}
String str1 = matter.format(date1); // 将date1格式化为自定义时间格式的字符串
String str2 = matter.format(date2); // 将date2格式化为自定义时间格式的字符串
String str3 = matter.format(date3); // 将date3格式化为自定义时间格式的字符串

System.out.println(str1); // 输出
System.out.println(str2); // 输出
System.out.println(str3); // 输出
System.out.println(date1.after(date2)); // 比较date1和date2

System.exit(0);
return;
}
From zhuocheng (cnblogs.com/zhuocheng)
posted on 2012-02-14 14:51  The Path of Growth  阅读(461)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3