首页 > 解决方案 > 如何获取字符串日期中的周数

问题描述

如果我有一个字符串日期,如“04/06/2019 5:11:37 PM”、“09/03/”,如何获取一个月中的周数和一个月中某个日期的周数2019 年 10 点 47 分 43 秒”、“2019 年 1 月 1 日上午 1 点 36 分 39 秒”。

例如,2019 年 9 月 3 日。三月的周数是:x 09 三月是周数:x

标签: android

解决方案


首先包括您的输入格式

String input = "2019/03/09";
String format = "yyyyMMdd";

SimpleDateFormat df = new SimpleDateFormat(format);
Date date = df.parse(input);
// you can parse it as Calendar object

Calendar cal = Calendar.getInstance();
cal.setTime(date);
int week = cal.get(Calendar.WEEK_OF_YEAR);

推荐阅读