超强的时间格式

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;import java.net.URLEncoder;
import java.sql.Timestamp;


public class CurrentDateTime {
 private SimpleDateFormat dFormat;

 private Date date;

 /**
  * 获取当前日期时间,自定义格式
  *
  * @param String
  * @return datetime
  * @日期 【G ※ 年代】,【y ※ 年】,【M ※ 月】,【d ※ 日】
  * @时间 【a ※ 上午 / 下午】,【h ※ (1~12)时】,【H ※ (0~23)时】,【m ※ 分】,【s ※ 秒】,【S ※ 毫秒】
  * @星期 【E ※ 星期】,【F ※ 一月中第几个星期几】,【w ※ 一年中第几个星期】,【W ※ 一月中第几个星期】
  * @其他 【D ※ 一年中的第几天】,【z ※ 时区】,【k ※ (1~24)时】,【K ※ (0~11)时】
  */
 public String getDateTime(String format) {
  date = new Date(new Date().getTime());
  dFormat = new SimpleDateFormat(format);
  String datetime = dFormat.format(date);
  return datetime;
 }

 /**
  * 获取当前日期
  *
  * @return date
  */
 public String getDate() {
  dFormat = new SimpleDateFormat("yyyy-MM-dd");
  date = new Date(new Date().getTime());
  String datestr = dFormat.format(date);
  return datestr;

 }

 /**
  * 获取当前日期时间
  *
  * @return Timestamp
  */
 public String getTimestamp() {
  Timestamp time = new Timestamp(System.currentTimeMillis());
  return time.toString();

 }

 /**
  * 获取当前日期时间
  *
  * @return UrlTimestamp
  */
 public String getUrlTimestamp() {
  Timestamp time = new Timestamp(System.currentTimeMillis());
  return URLEncoder.encode(time.toString());

 }

 /**
  * 获取某年某月的天数
  *
  * @param year
  * @param month
  * @return
  */
 public Integer getLastDayOfMonth(String year, String month) {
  Integer Year, Month;
  if (year != null && month != null) {
   Year = Integer.valueOf(year);
   Month = Integer.valueOf(month);
   Calendar c = Calendar.getInstance();
   c.set(Year, Month, 1);
   c.add(c.DAY_OF_YEAR, -1);
   return c.get(c.DAY_OF_MONTH);
  } else {
   System.out.println("年月不能为空:" + year + "年" + month + "月");
   return 0;
  }
 }

 public static void main(String[] args) {
  CurrentDateTime current = new CurrentDateTime();
  System.out.println(current.getDateTime("yyyy"));
  System.out.println(current.getDateTime("ww"));
 }

实例化,想得到什么样的格式,随便你!!!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值