Java中获取系统时间

public String getCurrentTime(){
TimeZone tz = TimeZone.getDefault();
Calendar ca = Calendar.getInstance(tz);
String strYear = String.valueOf(ca.get(Calendar.YEAR));
String strMonth = String.valueOf(ca.get(Calendar.MONTH) + 1);
String strDate = String.valueOf(ca.get(Calendar.DATE));
String SysDate = null;
String hour, minute, second;

if ((ca.get(Calendar.MONTH) + 1) <= 9 && ca.get(Calendar.DATE) <= 9) {
SysDate = strYear + "0" + strMonth + "0" + strDate;
}
else if ((ca.get(Calendar.MONTH) + 1)>9 && ca.get(Calendar.DATE) > 9) {
SysDate = strYear + strMonth + strDate;
}
else if ((ca.get(Calendar.MONTH) + 1) <= 9 && ca.get(Calendar.DATE) > 9) {
SysDate = strYear + "0" + strMonth + strDate;
}
else if ((ca.get(Calendar.MONTH) + 1) > 9 && ca.get(Calendar.DATE) <= 9) {
SysDate = strYear + strMonth + "0" + strDate;
}

if (ca.get(Calendar.HOUR_OF_DAY)<10) {
hour = "0" + String.valueOf(ca.get(Calendar.HOUR_OF_DAY));
}
else {
hour = String.valueOf(ca.get(Calendar.HOUR_OF_DAY));
}

if (ca.get(Calendar.MINUTE)<10) {
minute = "0" + String.valueOf(ca.get(Calendar.MINUTE));
}
else {
minute = String.valueOf(ca.get(Calendar.MINUTE));
}

if (ca.get(Calendar.SECOND)<10) {
second = "0" + String.valueOf(ca.get(Calendar.SECOND));
}
else {
second = String.valueOf(ca.get(Calendar.SECOND));
}

SysDate = SysDate + hour + minute + second;
return SysDate;

}


Java代码
  1. import java.text.SimpleDateFormat;   
  2. import java.util.Calendar;   
  3. import java.util.Date;   
  4. import java.util.GregorianCalendar;   
  5.   
  6. public class DateDeal {   
  7.   
  8.  /**  
  9.   * 将当前日期返回"yyyy-MM-dd"的字符串表现形式。  
  10.   *   
  11.   * @return 返回当前日期的"yyyy-MM-dd"的字符串表现形式。  
  12.   */  
  13.  public static String getCurrentDate() {   
  14.   SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");   
  15.   return df.format(new Date());   
  16.  }   
  17.   
  18.  /**  
  19.   * 将当前日期返回"yyyy年MM月dd日 HH:mm:ss"的字符串表现形式。  
  20.   *   
  21.   * @return 返回当前日期的"yyyy年MM月dd日 HH:mm:ss"的字符串表现形式。  
  22.   */  
  23.  public static String getCurrentTime() {   
  24.   SimpleDateFormat df = new SimpleDateFormat("yyyy'年'MM'月'dd'日' HH:mm:ss");   
  25.   return df.format(new Date());   
  26.  }   
  27.   
  28.  /**  
  29.   * 将Date的日期返回"yyyy-MM-dd HH:mm:ss"的字符串表现形式。  
  30.   *   
  31.   * @param date  
  32.   *            Date对象。  
  33.   * @return 返回"yyyy-MM-dd HH:mm:ss"的字符串表现形式。  
  34.   */  
  35.  public static String getDate(Date date) {   
  36.   SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");   
  37.   return df.format(date);   
  38.  }   
  39.   
  40.  /**  
  41.   * 根据当前日期返回不同形式的字符串形式。  
  42.   *   
  43.   * @param date  
  44.   *            Date对象。  
  45.   * @return 如果与当前时间所在年月日相同,则返回"HH:mm:ss"形式,否则返回"yyyy-MM-dd HH:mm:ss"。  
  46.   */  
  47.  public static String getDate2(Date date) {   
  48.   GregorianCalendar g1 = new GregorianCalendar();   
  49.   g1.setTime(date);   
  50.   GregorianCalendar g2 = new GregorianCalendar();   
  51.   g2.setTime(new Date());   
  52.   SimpleDateFormat df = null;   
  53.   if (g1.get(Calendar.YEAR) == g2.get(Calendar.YEAR)   
  54.     && g1.get(Calendar.MONTH) == g2.get(Calendar.MONTH)   
  55.     && g1.get(Calendar.DAY_OF_MONTH) == g2   
  56.       .get(Calendar.DAY_OF_MONTH))   
  57.    df = new SimpleDateFormat("HH:mm:ss");   
  58.   else  
  59.    df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");   
  60.   return df.format(date);   
  61.  }   
  62. }   

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值