开发者社区> 问答> 正文

将日期字符串转换为整数数组

我有一个格式为“ YYYY-MM-DD”的字符串,我想将其转换为整数数组

这是我的代码:

int year, month, day;
Date date = Calendar.getInstance().getTime();
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
String strDate = dateFormat.format(date);

例如,如果日期是2017年3月16日,我希望我的数组像[2017,3,16]。

问题来源:Stack Overflow

展开
收起
montos 2020-03-23 16:03:13 574 0
1 条回答
写回答
取消 提交回答
  • 首先,我要说的使用LOCALDATE的DateTimeFormatterJava的时间 API,并停止使用日历SimpleDateFormat的遗产日期类

    LocalDate currentDate = LocalDate.now();
    String dateString = currentDate.format(DateTimeFormatter.ISO_DATE);
    

    如果要将日期字符串转换为int数组,则split基于-。

    Integer[] array = 
        Arrays
        .stream( dateString.split("-") )
        .map( Integer::valueOf )
        .toArray( Integer[]::new )
    ;
    

    回答来源:Stack Overflow

    2020-03-23 16:04:54
    赞同 展开评论 打赏
问答地址:
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载

相关实验场景

更多