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.
Related
I'm very new to Java programming and I have string like this:
2013-03-15T07:23:13Z
I wish I could convert this into date format like:
15-03-2013
is that possible?
Thanks in advance.
Take the reference to this link
How can I change the date format in Java?
See the answer given by Mr. Christopher Parker
It has explained all your needs and it will provide you the easiest solution which is logically correct
Try this :
try {
DateFormat sourceDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
DateFormat targetFormat = new SimpleDateFormat("dd-MM-yyyy");
String strSourceDate = "2013-03-15T07:23:13Z";
Date targetDate = (Date) sourceDateFormat.parseObject(strSourceDate);
String strTargetDate = targetFormat.format(targetDate);
System.out.println(strTargetDate);
} catch (ParseException e) {
e.printStackTrace();
}
If the format of the input string is fixed, the simplest and the most expedient way of doing this would be with string manipulation:
String s = "2013-03-15T07:23:13Z";
String res = s.substring(8, 10)+"-"+s.substring(5, 7)+"-"+s.substring(0, 4);
It would spare you dealing with dates and calendars. Here is a demo on ideone.
java.text.SimpleDateFormat is what you need: SimpleDateFormat JavaDoc
You'll need one format to convert your input String into a Date using the parse() method, and then another to convert that Date into a String in your desired format using format().
If your application could be used internationally, don't forget to think about correctly localizing the output of the second function though. 03-11-2013 is March 11th in some countries, November 3rd in others.
Try this:
Date dNow = new Date( );
SimpleDateFormat ft =
new SimpleDateFormat ("dd.MM.yyyy");
System.out.println("Current Date: " + ft.format(dNow));
It's output
Current Date: 15.03.2013
Using SimpleDateFormat
String strDate = "2013-03-15T07:23:13Z";
SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");
String date = dateFormat.format(strDate);
System.out.println("Today in dd-MM-yyyy format : " + date);
Hope it help you...
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(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);
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();
}