注:转载自:https://www.cnblogs.com/-cyb/articles/Android_Date_format_Change.html
java8中,使用LocalDate、LocalTime、LocalDateTime用来处理时间。
//获取当前时间 LocalDate today = LocalDate.now(); //标准时间 LocalDateTime lt = LocalDateTime.now();
1、使用SimpleDateFormat
Date d=new Date(); SimpleDateFormat sdf=newSimpleDateFormat("YYYY-MM-DD : HH:MM:SS"); sdf.format(d);
2、字符串转换日期类型
String date="2019年02月13日"; SimpleDateFormat sdf=new SimpleDateFormat("yyyy年MM月dd日"); Date d=sdf.parse(date);
3、毫秒转换成日期类型
long l=System.currentTimeMillis(); Date d=new Date(l);
4、获取系统日期和时间,并转换成sql保存到数据库中
Date d=new Date(); //获取当前系统的时间 SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); //格式化日期 String dateStr = sdf.format(d); //转为字符串 //使用RS更新数据库,仍然要用rs.updateString,而不是rs.updateDade。 rs.updateString("regtime",dateStr); //regtime字段为datetime类型的
5、按照本地时区输出当前日期
Date d = new Date(); d.toLocaleString();
6、数据库中的日期以年-月-日方式输出
SimpleDateFormat sdf = new SimpleDateFormat(YYYY-MM-dd); String sqlStr = "select bookDate from roomBook where bookDate between '1993-4-10' and '2019-4-25'"; df.format(rs.getDate("bookDate"));
7、格式化小数
DecimalFormat df = new DecimalFormat(",###.00"); double aNumber = 111215198.289648989; String result = df.format(aNumber);