Android,Java,JavaScript,获取当前时间,YYYY-MM-DD HH:MM:SS

js

function getNowTime() {
    var date = new Date();
    var seperator1 = "-";
    var seperator2 = ":";
    var month = date.getMonth() + 1;
    var strDate = date.getDate();
    if (month >= 1 && month <= 9) {
        month = "0" + month;
    }
    if (strDate >= 0 && strDate <= 9) {
        strDate = "0" + strDate;
    }
    var time= date.getFullYear() + seperator1 + month + seperator1 + strDate
            + " " + date.getHours() + seperator2 + date.getMinutes()
            + seperator2 + date.getSeconds();
    return time;
}

Java

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String time = sdf.format( new  Date());

Android

第一种方法

SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
//获取当前时间
Date date = new Date(System.currentTimeMillis());
String time = simpleDateFormat.format(date)

第二种方法

Calendar calendar = Calendar.getInstance();
//年
int year = calendar.get(Calendar.YEAR);
//月
int month = calendar.get(Calendar.MONTH);
//日
int day = calendar.get(Calendar.DAY_OF_MONTH);
//时
int hour = calendar.get(Calendar.HOUR_OF_DAY);
//分
int minute = calendar.get(Calendar.MINUTE);
//秒
int second = calendar.get(Calendar.SECOND);
String time = "+year+'年'+month+'月'+day+'日'+hour+':'+minute+':'+second+";



你可能感兴趣的:(Java开发)