java 新人求大神帮忙啦

爱飞的云 2016-08-02 06:42:55
怎样把截取到的字符串转化为日期型的?》
比如:我输入 String型 身份证号 341221199509181757
截取到生日那段字符后是 19950918
问题来了 怎样把它转化成日期型的 1995年 09月18 日 ?
...全文
366 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
walkuere 2016-08-06
  • 打赏
  • 举报
回复
哦我错了,没看到身份证是楼主的。。。
walkuere 2016-08-06
  • 打赏
  • 举报
回复
不是我要说处女座,很感谢4楼的朋友帮我解决过问题 但是我生活中工作中碰到过一些处女座,实在是让人无法理解,比如,玩消消乐他是全部三星通关,其他各种强迫症也是很多,没的说,总之,要求很高 见识到4楼了,看到这样的问题我们想的最简单的办法当然是substring定好位置就好了,他居然用正则表达式,而且这样可以预防字符串太短报错和一些其他问题比如数据输入不对,看他代码里生日是处女座 果然处女座强迫症和别人不一样
  • 打赏
  • 举报
回复
没想那么高端的代码,,,我用最原始的代码,,希望能够帮到您! package day1; import java.util.Scanner; public class test { public static void main(String[] arges) { Scanner scan = new Scanner(System.in); System.out.println("请输入您的身份证:"); String str = scan.next(); String str1 = str.substring(6,10); String str2 = str.substring(10,12); String str3 = str.substring(12,14); System.out.println(str1+"年"+str2+"月"+str3+"日"); } }
walkuere 2016-08-05
  • 打赏
  • 举报
回复
引用 4 楼 rickylin86 的回复:

import java.util.regex.Pattern;
import java.util.regex.Matcher;

public class Test{
	public static void main(String[] args){
		String id = "341221199509181757";
		String regex = "^\\d{6}(?<year>\\d{4})(?<month>\\d{2})(?<day>\\d{2}).+$";
		Matcher matcher = Pattern.compile(regex).matcher(id);
		if(matcher.matches()){
			System.out.println(String.format("%s年%s月%s日",matcher.group("year"),matcher.group("month"),matcher.group("day")));
		}
	}
}
果然您是处女座的,用正则表达式,像我等没有强迫症的,substring方便就用substring了
ncist_jianeng 2016-08-05
  • 打赏
  • 举报
回复
Date d = new SimpleDateFormat("yyyyMMdd").parse(date); new SimpleDateFormat("yyyy年MM月dd日").format(d);
fondOfJava 2016-08-05
  • 打赏
  • 举报
回复
字符串截取可以处理。
Be_nurturing 2016-08-05
  • 打赏
  • 举报
回复
身份证的的长度是固定的,而且哪位对应是什么意思,也好理解,只需要对应的对字符串进行截取,然后年月日之间添加对应的数字就可以了
rickylin86 2016-08-02
  • 打赏
  • 举报
回复

import java.util.regex.Pattern;
import java.util.regex.Matcher;

public class Test{
	public static void main(String[] args){
		String id = "341221199509181757";
		String regex = "^\\d{6}(?<year>\\d{4})(?<month>\\d{2})(?<day>\\d{2}).+$";
		Matcher matcher = Pattern.compile(regex).matcher(id);
		if(matcher.matches()){
			System.out.println(String.format("%s年%s月%s日",matcher.group("year"),matcher.group("month"),matcher.group("day")));
		}
	}
}
凌寒11 2016-08-02
  • 打赏
  • 举报
回复
楼上正解~~~~~。。。
阳光越来越暖 2016-08-02
  • 打赏
  • 举报
回复
Date date = new Date(); String dateStr = new SimpleDateFormat("yyyy-MM-dd").format(date);
shuaige112500 2016-08-02
  • 打赏
  • 举报
回复
是要改成String类型的“1995年 09月18 日“吧?

62,628

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧