Java获取当前时间方法总结
程序员文章站
2022-04-14 15:09:36
最简单获取当前时间的方法:
方法一:
import java.util.calendar;
public class datetime{
public s...
最简单获取当前时间的方法:
方法一:
import java.util.calendar; public class datetime{ public static void main(string[] args){ calendar c = calendar.getinstance(); system.out.println("当前时间为:"); system.out.println(c.get(calendar.year)+"年"+(c.get(calendar.month)+1)+"月"+c.get(calendar.date)+"日"); system.out.println(c.get(calendar.hour)+"点"+c.get(calendar.minute)+"分"+c.get(calendar.second)+"秒"); system.out.println("今天是一年中的第:" + c.get(calendar.day_of_year) + "天"); system.out.println("今天是一年中的第:" + c.get(calendar.week_of_year) + "个星期"); } }
方法二:
import java.util.date; import java.text.simpledateformat; public class nowstring { public static void main(string[] args) { simpledateformat df = new simpledateformat("yyyy-mm-dd hh:mm:ss");//设置日期格式 system.out.println(df.format(new date()));// new date()为获取当前系统时间 } }
方法三:
import java.util.date; import java.util.calendar; import java.text.simpledateformat; public class testdate{ public static void main(string[] args){ date now = new date(); simpledateformat dateformat = new simpledateformat("yyyy/mm/dd hh:mm:ss");//可以方便地修改日期格式 string hehe = dateformat.format( now ); system.out.println(hehe); calendar c = calendar.getinstance();//可以对每个时间域单独修改 int year = c.get(calendar.year); int month = c.get(calendar.month); 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); system.out.println(year + "/" + month + "/" + date + " " +hour + ":" +minute + ":" + second); } }
上一篇: java计算两点间的距离方法总结