java 获取当前系统时间

本文介绍如何使用Java进行日期和时间的操作,包括获取当前系统时间、日期加减、格式化日期等常见需求,并提供了详细的代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

     Calendar c = Calendar.getInstance();  
        c.setTime(new Date());   //设置当前日期  
        c.add(Calendar.DATE, 1); //日期分钟加1,Calendar.DATE(天),Calendar.HOUR(小时)  
        Date date = c.getTime(); //结果  
        System.out.println(date);  
        //结果Tue Oct 01 17:34:52 CST 2013

//    获得上一个月的年和月,要求格式为2008-05
    Calendar   cal=Calendar.getInstance();//当前日期   
    cal.set(Calendar.DATE,1);//设为当前月的1号   
    cal.add(Calendar.DATE,-1);//减一天,变为上月最后一天 
    SimpleDateFormat   simpleDateFormat   =   new   SimpleDateFormat("yyyy-MM");   
    System.out.println(simpleDateFormat.format(cal.getTime()));  


        //获取明天的日期
        Date date1=new Date();//取时间
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(date1);
        calendar.add(calendar.DATE,1);//把日期往后增加一天.整数往后推,负数往前移动
        date1=calendar.getTime(); //这个时间就是日期往后推一天的结果        
        SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
        String dateString = formatter.format(date1);
          
        System.out.println(dateString);
        //结果是20131001    当前时间是20130930
 

java 获取当前系统时间

  51人阅读  评论(0)  收藏  编辑  删除
获取系统当前时间:
public static void main(String[] args) {
SimpleDateFormat tempDate = new SimpleDateFormat("yyyy-MM-dd");
String datetime = tempDate.format(new java.util.Date());
System.out.println(datetime);

}

显示:2012-05-16



public static void main(String[] args) {

SimpleDateFormat tempDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String datetime = tempDate.format(new java.util.Date());
System.out.println(datetime);
}

显示:2013-05-16 12:12:56


  1.  long now = System.currentTimeMillis(); // 现在经过的毫秒数  
  2.             long time = now / 1000;// 现在经过的秒数  
  3.             long time2 = time - 2 * 24 * 60 * 60;// 少两天的天数
  4.  String sql = "select GlobalID from "  
  5.                             + ch.getTableName()  
  6.                             + " where Status=1 and ChannelCode like '"+ch.getChannelCode()+"%' and CreateDate>"  
  7.                             + time2+" order by PublishDate desc";  


指定的时间加上一分钟
String str="1259";
SimpleDateFormat sdf= new SimpleDateFormat("HHmm");
Date birthday = sdf.parse(str);
System.out.println(birthday);
  System.out.println((new SimpleDateFormat("HHmm")).format(birthday));
  Calendar cal = Calendar.getInstance();
  cal.setTime(birthday);
  cal.add(Calendar.MINUTE, 1);
  String a=(new SimpleDateFormat("HHmm")).format(cal.getTime());
  System.out.println(a);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值