0

My problem is that the array ms[ ] doesn't get values when I do split( ); Why is this happening ?

public class Test {

    public static void main(String[] args) {
        Date date = new Date();
        SimpleDateFormat ft = new SimpleDateFormat("yyyy.MM.dd.HH.mm.ss.");    //change format          
        String msgTime = ft.format(date);
        System.out.println(msgTime);

        String ms[] = msgTime.split(".");
        System.out.println(ms.length);
    }
} 
rai.skumar
  • 10,309
  • 6
  • 39
  • 55
alexca
  • 57
  • 7

2 Answers2

1

The problem is split() function takes regular expression as argument, not a simple string. And "." regular expression means "any symbol". So you need just escape it.

String ms[] = msgTime.split("\\.");

infthi
  • 537
  • 1
  • 5
  • 19
0

I'm guessing you meant to do

String ms[] = msgTime.split("\\.");

String.split() takes a regular expression so you should escape any special characters, such as ..

cklab
  • 3,761
  • 20
  • 29