广州北大青鸟计算机职业培训学校
互联网技术培训、软件技术培训、大数据培训、云计算培训、数据分析培训信息网
当前位置:网站首页 > 软件教程 > Java技术 > 正文

惠州Java如何解析单个字符串中的两个日期值?_北大青鸟IT学校

作者:邓华发布时间:2021-04-20分类:Java技术浏览:733


导读:Java如何解析单个字符串中的两个日期值?最近惠州北大青鸟很多学员都在问这个问题,那么下面惠州北大青鸟学术部老师给大家解答一下这个问题。

Java如何解析单个字符串中的两个日期值?最近惠州北大青鸟很多学员都在问这个问题,那么下面惠州北大青鸟学术部老师给大家解答一下这个问题。

以下代码解析单个字符串中的两个日期值。

import java.text.ParsePosition;import java.text.SimpleDateFormat;import java.util.Date;public class Main {  public static void main(String[] args) {
   String text = "ab01/01/1999cd12/31/2000ef";
   String pattern = "MM/dd/yyyy";

   SimpleDateFormat simpleFormatter  = new SimpleDateFormat(pattern);    // Set  the   start index   at 2    ParsePosition startPos  = new ParsePosition(2);    // Parse the   text to get   the   first date (January 1,  1999)
   Date  firstDate = simpleFormatter.parse(text, startPos);
   System.out.println(firstDate);   //Now, startPos has  its index set after the last  character of the first date parsed.  
   int  currentIndex = startPos.getIndex();
   System.out.println(currentIndex);    // To set its index   to the   next   date increment its index   by  2.

   int  nextIndex = currentIndex + 2;
   startPos.setIndex  (nextIndex);    // Parse the   text to get   the   second  date (December  31,   2000)
   Date  secondDate = simpleFormatter.parse(text, startPos);
   System.out.println(secondDate);

 }

}

上面的代码生成以下结果。

Fri Jan 01 00:00:00 PST 1999

12

Sun Dec 31 00:00:00 PST 2000

想了解更多关于Java的资讯,可以来惠州北大青鸟新方舟校区了解一下。

java5.png

Java

标签:惠州计算机JAVA软件开发惠州计算机Java软件开发惠州计算机JAVA培训惠州计算机JAVA软件开发学校惠州计算机Java软件开发培训JAVAJava软件开发北大青鸟IT计算机学校北大青鸟IT软件学校北大青鸟IT学校


Java技术排行
标签列表
网站分类
文章归档
最近发表