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 + "-" + . . .
Related
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'));
I have String
String aa="01:30";
String hh=aa.substring(0,2);
String mm=aa.substring(3,5);
I am trying to parse the seperated values by using
int hh=Integer.parseInt(hhs);
int mm=Integer.parseInt(mms);
The out put is 1 and 30 How can I solve to get output as it is like 01 & 30?
Thanks in advance
An integer can't store leading zeroes. If you are getting 3 for mm though it indicates you have another problem, as that should resolve to 30
Saying you want an integer value and you want a leading zero is contradictory. Numeric data types have just the number value; only a String representation of that number has a leading zero.
So you need to decide… Do you want:
An integer (example: 1)
A String (example: 01)
A time (example: 01:30)
Generally if working with date-time values, you should treat them as such. Rather than use the notoriously troublesome java.util.Date/Calendar classes, use either the Joda-Time library or the new java.time.* classes in Java 8.
Example code using Joda-Time 2.3.
String input = "01:30";
LocalTime localTime = new LocalTime( input );
int hourOfDay = localTime.getHourOfDay();
int minuteOfHour = localTime.getMinuteOfHour();
Dump to console…
System.out.println( "localTime: " + localTime );
System.out.println( "hourOfDay: " + hourOfDay );
System.out.println( "minuteOfHour: " + minuteOfHour );
When run…
localTime: 01:30:00.000
hourOfDay: 1
minuteOfHour: 30
You can always do formatting
like this
DecimalFormat df=new DecimalFormat("00");
System.out.println("HH "+df.format(hh));
That will give 01 as the output.
String aa="01:30";
String hhs=aa.substring(0,2);
String mms=aa.substring(3,5);
int hh=Integer.parseInt(hhs);
int mm=Integer.parseInt(mms);
String hh_formatted = String.format(Locale.ENGLISH,"%02d", hh);
String mm_formatted = String.format(Locale.ENGLISH,"%02d", mm);
Using above only you can get your desired output in string format.
Time specific things should be done with Date()..
SimpleDateFormat inFormat = new SimpleDateFormat("HH:mm");
SimpleDateFormat outHourFormat = new SimpleDateFormat("HH");
SimpleDateFormat outMinFormat = new SimpleDateFormat("mm");
String outHour = null;
String outMinute = null;
Date input;
try {
input = inFormat.parse("03:30");
outHour = outHourFormat.format(input);
outMinute = outMinFormat.format(input);
} catch (ParseException e) {
e.printStackTrace();
}
// now you got two Strings: outHour & outMinute in correct form.
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.
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(" .*", "");
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   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();
}