admin 管理员组文章数量: 887053
2024年1月14日发(作者:eventhandler的使用)
import ateTime;public static void main(String[] args) { LocalDateTime today = (); n(today);}输出:2021-04-12T16:37:25.244获取当前时间(Timestamp类型)import amp;import DateFormat;public static void main(String[] args) { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Timestamp timestamp = new Timestamp(tTimeMillis()); String now = (timestamp); n(now);}输出:2021-04-12 17:06:33Date类型 与 String类型转换import xception;import DateFormat;import ;public static void main(String[] args) throws ParseException { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); //Date转String Date date = new Date(); String now = (date); n(now); //String转Date Date now2 = ("2021-04-12 17:30:54"); n(now2);}输出:2021-04-12 16:55:59DateTime类型 转 String类型public static void main(String[] args) { DateTime dateTime = new DateTime(); String nowTime = ng("yyyy-MM-dd HH:mm:ss"); n(nowTime);}输出:2021-04-12 16:54:09
Date类型 转 Instant类型import t;import ;public static void main(String[] args) { Date date = new Date(); Instant instant = ant(); n(instant);}输出:2021-04-12 16:54:09DateTime类型 与 Calendar类型转换import ar;import ;public static void main(String[] args) { //Calendar转化为Date Calendar cal = tance(); Date date = e(); n(date); //Date转化为Calendar Date date2=new Date(); Calendar cal2 = tance(); e(date2); n(cal2);}输出:Mon Apr 12 17:27:11 CST 2021输
出:ianCalendar[time=02,areFieldsSet=true,areAllFieldsSet=true,lenient=true,zone=aString类型 与 Instant类型转换
import t;import ateTime;import ;import meFormatter;import ;public static void main(String[] args) { //Instant转String DateTimeFormatter dtf = ern("yyyy-MM-dd HH:mm:ss"); Instant instant = (); ZoneId zoneId = Default(); LocalDateTime localDateTime = ant(instant, zoneId); String now = (localDateTime); n(now); //String转Instant String string = "2021-04-12T23:59:59.123Z"; DateTimeFormatter dtf2 = ern("yyyy-MM-dd'T'HH:mm:'Z'", ); LocalDateTime date = (string, dtf2); n(date);}输出:2021-04-12 17:01:33LocalDate类型 转换 Date类型import t;import ate;import ;import ;public static void main(String[] args) { LocalDate localDate = (); ZoneId zone = Default(); Instant instant = tOfDay().atZone(zone).toInstant(); Date date = (instant); n(date);}输出:Mon Apr 12 00:00:00 CST 2021
版权声明:本文标题:Java日期时间各种格式类型转换 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.freenas.com.cn/free/1705201997h476763.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论