How to remove anything from string after whitespaces in java - java

I have a String like this: 12/16/2011 12:00:00 AM
now i want to show only date part i.e 12/16/2011 on Textview
and remove the other part. What shall i need to do for this??
Any help will be appricated
Thanks.

Use java.text.DateFormat to parse the String into a Date and then reformat to display it as you wish with another DateFormat:
DateFormat inputFormat = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss a");
inputFormat.setLenient(false);
DateFormat outputFormat = new SimpleDateFormat("MM/dd/yyyy");
outputFormat.setLenient(false);
Date d = inputFormat.parse("12/16/2011 12:00:00 AM");
String s = outputFormat.format(d);

String str = "11/12/2011 12:20:10 AM";
int i = str.indexOf(" ");
str = str.substring(0,i);
Log.i("TAG", str);

Just two simple possibilities:
String str = "12/16/2011 12:00:00 AM";
// method 1: String.substring with String.indexOf
str.substring(0, str.indexOf(' '));
// method 2: String.split, with limit 1 to ignore everything else
str.split(" ", 1)[0];

you can use this below code to get sub string
String thisString="Hello world";
String[] parts = theString.split(" ");
String first = parts[0];//"hello"
String second = parts[1];//"World"

myString = myString.substring(0, str.indexOf(" "));
or
myString = myString.split(" ", 1)[0];

using a regular expression (more robust than others - works even when there is no whitespace found)
str.replaceAll(" .*", "");

Related

Splitting a Java String

I have a YYYYMM date in String format. I want to split it into YYYY and MM and replace the existing YYYY with a new year and concatenate it back in Java.
For example, I have 201201. I want to split it into 2012 and 01 and change 2012 to 2000 and finally get 200001. How do I do it?
I googled it but everyone seemed to have a - or a * in between.
Or if anyone knows a better way to do it (maybe change it into a Date and modify the year), I am all ears.
Any help would be appreciated!
Thanks
Take a look to this code to see if it can help you.
String myDate = "201201";
String myDate1 = myDate.substring(0, 4);
String myDate2 = myDate.substring(4);
myDate1 = "2000"; //Change the logic accordingly with your spec
System.out.println(myDate1 + myDate2);
One uber-simple approach is just to use string manipulation:
String orig = "201201";
String changed = "2000" + orig.substring(4);
EDIT:
A looping example, as per the comment:
// Loop over the inputs
for (String date : getDatesFromFile() {
// Loop over 10 years:
for (int year = 2000; year <= 2010; ++year) {
String newDate = String.valueOf(year) + date.substring(4);
// write newDate to an output file
}
}
try This :
String Str = new String("201201");
System.out.print("Replace Year :" );
System.out.println(Str.replace('2012', '2000'));

How to cut and replace String

I have a following string which I have to split and replace with "/" on some condition**
String date = "20131105";
I want to change these string to "2013/11/05"
Edit:
I mean variable date must be String not Date data type
Do like this
Date date = new SimpleDateFormat("yyyyMMdd").parse("20131105");
String formattedDate = new SimpleDateFormat("yyyy/MM/dd").format(date);
System.out.println(formattedDate);
Output
2013/11/05
Use the substring method.
date = date.substring(0, 4) + "/" + date.substring(4, 6) + "/" + date.substring(6, 8);
try this
String date = "20131105";
String date1=date.substring(0, 4);
String date2=date.substring(4,6);
String date3=date.substring(6,8);
System.out.println(date1+"/"+date2+"/"+date3);
output 2013/11/05
See you have many logics .. you can use any one..
for example answer from prabhakaran which is
Date date = new SimpleDateFormat("yyyyMMdd").parse("20131105");
String formattedDate = new SimpleDateFormat("yyyy/MM/dd").format(date);
System.out.println(formattedDate);
here you can do one change like this
Date date = new SimpleDateFormat("yyyyMMdd").parse(StringVaribale);
String formattedDate = new SimpleDateFormat("yyyy/MM/dd").format(date);
System.out.println(formattedDate);
here you are converting to date and then converting back to string
Another one is you can take a substring and add "/" into your string
in this logic you should take StringBuffer insteed of string. because this is having some extra feature.

Parse string with integer value to date

I have a string with this value, for example: "20130211154717" I want it to be like "2013-02-11 15:47:17". How can I do that?
You can use two SimpleDateFormat: one to parse the input and one to produce the output:
String input = "20130211154717";
Date d = new SimpleDateFormat("yyyyMMddhhmmss").parse(input);
String output = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(d);
System.out.println("output = " + output);
You can use regular expressions for that:
String formattedDate = plainDate.replaceFirst(
"(\\d{4})(\\d{2})(\\d{2})(\\d{2})(\\d{2})(\\d{2})",
"$1-$2-$3 $4:$5:$6");
Though, I like assylias's SimpleDateFormat answer better. :-)
What you want to use for this is a SimpleDateFormat. It has a method called parse()
You can use the substring() method to get what you want:
String data = "20130211154717";
String year = data.substring(0, 4);
String month = data.substring(4, 2);
// etc.
and then string them together:
String formatted = year + "-" + month + "-" + . . .

String.format to get the output date in dd-MMM-YYYY HH:mm pattern (Java)

String.format(start.toString("dd-MMM-YYYY HH:mm"));
where start is a date input in LocalDateTime class from org.joda.time api
when i am using this code, its returning month like this "Dec" but i want the output as "DEC".
If you want a specific case, I would use .toUpperCase()
If this (String.format(start.toString("dd-MMM-YYYY HH:mm"));) retrieves the correct format of what you want, then you can simply use
String.format(start.toString("dd-MMM-YYYY HH:mm")).toUpperCase();
I always using substring, in my case like this :
String sDate = String.format(start.toString("dd-MMM-YYYY HH:mm"));
String oDate = sDate.substring(0, 2)+"-"+sDate.substring(3, 6).toUppercase()+"-"+sDate.substring(7, 11);
The only way will be to replace a substring by the uppercase version.
Date start = new Date();
SimpleDateFormat oFormat = new SimpleDateFormat("dd-MMM-YYYY HH:mm");
String sDate = oFormat.format(start);
System.out.println(sDate);
sDate = sDate.substring(0,3) + sDate.substring(3,6).toUpperCase() + sDate.substring(6,sDate.length());
System.out.println(sDate);

How to convert date format in java?

I have a String a ="   December 2011   ";
There is a before and after it.
How can I Convert it to: String b = "2011-12-1"
If this is really all there is to it, use .replace():
String b = a.replace(" ", "");
Unlike what its name would let you believe since .replaceAll() exists, .replace() will actually replace all occurrences. The difference is that .replaceAll() expects a string which will be compiled as a Pattern as an argument.
Use SimpleDateFormat.
new SimpleDateFormat("yyyy-MM-d").format(new SimpleDateFormat("MMMMM yyyy").parse(str));
String a = " Dezember 2011 ";
SimpleDateFormat sdf = new SimpleDateFormat("MMMMM yyyy");
a = a.replace(" ", "");
Date parse = sdf.parse(a);
SimpleDateFormat outSdf = new SimpleDateFormat("yyyy-MM-d");
String out = outSdf.format(parse);
System.out.println(out);
You use this code.It gives whatever you want to output.
You first remove &nbsp using replace function and after you have to just parse the your string.
try {
String a = " December 2011 ";
a = a.replace(" ", "");
SimpleDateFormat sdf = new SimpleDateFormat("MMMMM yyyy");
SimpleDateFormat SdfParser = new SimpleDateFormat("yyyy-MM-dd");
String date =SdfParser.format(sdf.parse(a));
System.out.println(date);
} catch (ParseException e) {
e.printStackTrace();
}

Categories

Resources