Wednesday, 28 October 2015

Java : String date One format to another format

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class TestStringDate {
public static void main(String[] args) throws ParseException {
String strValue = "2015-07-05 19:52:00";
DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
DateFormat format1 = new SimpleDateFormat("MM/dd/yyyy hh:mm a");
Date d1 = format.parse(strValue);
System.out.println(d1);
System.out.println(format1.format(d1));
}
}
/*
OutPut:
Sun Jul 05 19:52:00 IST 2015
07/05/2015 07:52 PM
*/

Wednesday, 14 October 2015

Split string on the first white space occurence

  
If you only care about the space character (and not tabs or other whitespace characters) and only care about everything before the first space and everything after the first space, you can do it without a regular expression like this:

var str = "Hello India Jai Hind";
str.substr(0,str.indexOf(' ')); // "Hello"
str.substr(str.indexOf(' ')+1); // "India Jai Hind"

Note that if there is no space at all, then the first line will return an empty string and the second line will return the entire string. Be sure that is the behavior that you want in that situation (or that that situation will not arise).