Several methods of obtaining the current time in Java

import java.util.*;      
    
public class D      
{      
public static void main(String []abc)      
{      
    int y,m,d,h,mi,s;      
    Calendar cal=Calendar.getInstance();      
    y=cal.get(Calendar.YEAR);      
    m=cal.get(Calendar.MONTH);      
    d=cal.get(Calendar.DATE);      
    h=cal.get(Calendar.HOUR_OF_DAY);      
    mi=cal.get(Calendar.MINUTE);      
    s=cal.get(Calendar.SECOND);      
    System.out.println("现在时刻是"+y+"年"+m+"月"+d+"日"+h+"时"+mi+"分"+s+"秒");      
}      
    
}     
##########################################################################      
public class Main{      
    public static void main(String[] args){      
        java.util.Calendar c=java.util.Calendar.getInstance();      
        java.text.SimpleDateFormat f=new java.text.SimpleDateFormat("yyyy年MM月dd日hh时mm分ss秒");      
        System.out.println(f.format(c.getTime()));      
    }      
}    
  
  
##########################################################################     
  
public String GetNowDate(){     
    String temp_str="";     
    Date dt = new Date();     
    //最后的aa表示“上午”或“下午”    HH表示24小时制    如果换成hh表示12小时制     
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss aa");     
    temp_str=sdf.format(dt);     
    return temp_str;     
}   

one

Date day=new Date();

SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

System. out. println(df.format(day));

two

SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

System. out. println(df.format(System.currentTimeMillis()));

three

Calendar c = Calendar. getInstance();

int year=c.get(Calender.YEAR);

int month=c.get(Calender.MONTH);

int date=c.get(Calender.DATE);

int hour=c.get(Calender.HOUR_OF_DAY);

int minute=c.get(Calender.MINUTE);

int second=c.get(Calender.SECOND);

System. out. println(year+"/"+month+"/"+date+" "+hour+":"+minute+":"+second);

four

Date date=new Date();

String. format("%tY",date);

String year=String. format("%tY",date);

String month=String. format("%tB",date);

String day=String. format("%te",date);

System. out. Println ("today is:" + year + "-" + month + "-" + day);

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>