获得当前日期的两种方法以及获得当前月份的所有日期

js获取当前时间  yyyy-MM-dd hh:mm:ss

Date.prototype.Format = function (fmt) { // author: meizz
        var o = {
        "M+": this.getMonth() + 1, // 月份
        "d+": this.getDate(), // 日
        "h+": this.getHours(), // 小时
        "m+": this.getMinutes(), // 分
        "s+": this.getSeconds(), // 秒
        "q+": Math.floor((this.getMonth() + 3) / 3), // 季度
        "S": this.getMilliseconds() // 毫秒
    };
    if (/(y+)/.test(fmt))
        fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
    for (var k in o)
        if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
            return fmt;
}
        var Time = new Date().Format("yyyy-MM-dd hh:mm:ss");

一:需要引用java.util.date

Date now = new Date();

SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");//可以方便地修改日期格式
String hehe = dateFormat.format( now ); 

System.out.println(hehe); 

二:需要引用

Calendar java.util.Calendar.getInstance()

代码如下

Calendar c = Calendar.getInstance();//可以对每个时间域单独修改
int year = c.get(Calendar.YEAR); 
int month = c.get(Calendar.MONTH)+1; //获得的月份比当前少一,所以需要+1
int date = c.get(Calendar.DATE); 
//int hour = c.get(Calendar.HOUR_OF_DAY); 
//int minute = c.get(Calendar.MINUTE); 
//int second = c.get(Calendar.SECOND); 

String nowtime=year + "-" + month + "-" + date; 

三:当前月份的所有日期

                    List list = new ArrayList();
   Calendar aCalendar = Calendar.getInstance(Locale.CHINA);
   int year = aCalendar.get(Calendar.YEAR);//年份
   int month = aCalendar.get(Calendar.MONTH) + 1;//月份
   int day = aCalendar.getActualMaximum(Calendar.DATE);
   for (int i = 1; i <= day; i++) {
       String aDate = String.valueOf(year)+"-"+month+"-"+i;
       list.add(aDate);
   }

猜你喜欢

转载自blog.csdn.net/qq_40216244/article/details/81033907