java 有个时间是yyyy年MM月dd日中如何提取出单独的年,月,日

如题,请提供代码 谢谢了。
2025-03-06 13:42:15
推荐回答(3个)
回答1:

直接通过格式转换的形式即可。举例:

String str0 = "2015年07月05日";

Date d1 = new SimpleDateFormat("yyyy年MM月dd日").parse(str0);//定义起始日期

SimpleDateFormat sdf0 = new SimpleDateFormat("yyyy");

SimpleDateFormat sdf1 = new SimpleDateFormat("MM");

SimpleDateFormat sdf2= new SimpleDateFormat("dd");

String str1 = sdf0.format(d1);

String str2 = sdf1.format(d1);

String str3 = sdf2.format(d1);

System.out.println("年份为:"+str1);

System.out.println("月份为:"+str2);

System.out.println("日为:"+str3);

结果:

回答2:

public class DateDemo{
    public static void main(String[] args){
        String str = new java.text.SimpleDateFormat("yyyy年MM月dd日").format(new java.util.Date());
        String yeah = str.substring(0,4); //取年
        String yue = str.substring(str.indexOf("年")+1,str.indexOf("月")); //取月
        String ri = str.sbstring(str.indexOf("月")+1,str.indexOf("日")); //取日
    }
}

回答3:

使用SimpleDateFormate对数据进行格式化,然后拿到Date对象..最后从Date对象中取出单独的年月日..或者使用正则表达式进行切分.然后拿到数据