Rajesh Hatwar
Rajesh Hatwar

Reputation: 1933

How to parse UTC time stamp and convert into another format in java

Hi i have UTC time stamp and i need parse to convert it into IST format I use to receive UTC time stamp from MSC (Telecom base station) in the form of string : 1307261822062B0530 and it can be divide as bellow

13 07 26 18 22 06 2B 05 30

yy  =   13
MM  =   07
DD  =   26
hh  =   18
mm  =   22
ss  =   06
S   =   2B (how do iconvert this value into + / -)
hh  =   05 
mm  =   30

The problem is converting sign (+/-) to add or subtract on received universal time

i am trying to parse as bellow

public static String formatRawTimeStamp(String rawTimeStamp){

      String[] arr_msisdn = rawTimeStamp.split("(?<=\\G.{2})"); // split every two character  
      String formatedDate = "";
      Date date;
      DateFormat srcFormat = new SimpleDateFormat("yy-MM-dd HH:mm:ss.ssZ");
      DateFormat desFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

      if(arr_msisdn.length >= 6){

        try {

            date = (Date)srcFormat.parse(arr_msisdn[0]+"-"+arr_msisdn[1]+"-"+arr_msisdn[2]+" "+arr_msisdn[3]+":"+arr_msisdn[4]+":"+arr_msisdn[5]+"."+arr_msisdn[6]+""+arr_msisdn[7]+""+arr_msisdn[8]);
            formatedDate = desFormat.format(date);

        } catch (ParseException e) {

            e.printStackTrace();
        }
      }

      return formatedDate;
  }

But i am getting exception

java.text.ParseException: Unparseable date: "13-07-30 18:45:11.2b0530"
    at java.text.DateFormat.parse(DateFormat.java:354)
    at org.bouncycastle.asn1.util.ASNUtil.formatRawTimeStamp(ASNUtil.java:199)
    at org.bouncycastle.asn1.util.MOCallEvent.decode(MOCallEvent.java:187)
    at org.bouncycastle.asn1.util.ZTEASN1DecodeApp.decode(ZTEASN1DecodeApp.java:114)
    at org.bouncycastle.asn1.util.ZTEASN1DecodeApp.main(ZTEASN1DecodeApp.java:80)

Hear it unable to parse "2b" value it indicate sign (+/-)

S   =   Sign 0 = “+”, “-“       ASCII encoded

how can i solve this. any help will be appreciated.

Upvotes: 1

Views: 918

Answers (1)

assylias
assylias

Reputation: 328608

Why don't you simply replace the characters before parsing?

String input = "1307261822062B0530";
String adjusted = input.replaceAll("2[Bb]", "+").replaceAll("2[Aa]", "-");

DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyMMddHHmmssZ");
OffsetDateTime odt = OffsetDateTime.parse(adjusted, fmt); //2013-07-26T18:22:06+05:30

I have assumed that the input can contain 2B or 2b for + and 2A or 2a for -.

If you want to use a simpledateformat, the same logic can be applied.

Upvotes: 2

Related Questions