Discussions
Categories
- 196.9K All Categories
- 2.2K Data
- 239 Big Data Appliance
- 1.9K Data Science
- 450.4K Databases
- 221.7K General Database Discussions
- 3.8K Java and JavaScript in the Database
- 31 Multilingual Engine
- 550 MySQL Community Space
- 478 NoSQL Database
- 7.9K Oracle Database Express Edition (XE)
- 3K ORDS, SODA & JSON in the Database
- 546 SQLcl
- 4K SQL Developer Data Modeler
- 187K SQL & PL/SQL
- 21.3K SQL Developer
- 295.9K Development
- 17 Developer Projects
- 138 Programming Languages
- 292.6K Development Tools
- 107 DevOps
- 3.1K QA/Testing
- 646K Java
- 28 Java Learning Subscription
- 37K Database Connectivity
- 155 Java Community Process
- 105 Java 25
- 22.1K Java APIs
- 138.1K Java Development Tools
- 165.3K Java EE (Java Enterprise Edition)
- 18 Java Essentials
- 160 Java 8 Questions
- 86K Java Programming
- 80 Java Puzzle Ball
- 65.1K New To Java
- 1.7K Training / Learning / Certification
- 13.8K Java HotSpot Virtual Machine
- 94.3K Java SE
- 13.8K Java Security
- 204 Java User Groups
- 24 JavaScript - Nashorn
- Programs
- 442 LiveLabs
- 38 Workshops
- 10.2K Software
- 6.7K Berkeley DB Family
- 3.5K JHeadstart
- 5.7K Other Languages
- 2.3K Chinese
- 171 Deutsche Oracle Community
- 1.1K Español
- 1.9K Japanese
- 232 Portuguese
making a new string from old string

I have two String .
String str1="01/12/2016"
I want to convert into another String str2="20161201"
One way I can think of is to split the string and then append from backwards to generate this new string. Is there any easy way one can make it ?
and ...
String str3="13/12/2016"
I want to convert into another string str4="20161214"
I can think of it as split and then add like this ...
"2016"+"12"+ "convert to integer (13) + 1"
Is there any easy way one can make it ?
Answers
-
user575089 wrote:I have two String .String str1="01/12/2016" I want to convert into another String str2="20161201"One way I can think of is to split the string and then append from backwards to generate this new string. Is there any easy way one can make it ?and ...String str3="13/12/2016" I want to convert into another string str4="20161214"I can think of it as split and then add like this ..."2016"+"12"+ "convert to integer (13) + 1" Is there any easy way one can make it ?
When you have questions about basic Java functionality the FIRST place to look for information is The Java Tutorials. There are trails that cover all of the basics and they have working code.
If you insist that you have 'strings' then just use string functionality. You can use the 'String.split' method to split strings into an array of components and then construct a new string from the components.
https://docs.oracle.com/javase/tutorial/java/data/strings.html
Strings
Strings, which are widely used in Java programming, are a sequence of characters. In the Java programming language, strings are objects.
Just read that and the related trails and try the example code. Should be simple.
I can think of it as split and then add like this ..."2016"+"12"+ "convert to integer (13) + 1"
No - you can NOT add 'strings'. You add 'numbers'. You can add numbers to other numbers and you can add some numeric values to 'date' values.
So if you REALLY have strings that are supposed to represent date values then the FIRST step is to make sure your string REALLY represents a valid date value.
The Java Tutorials has trails that discuss date and time values and how to work with them.
Dates and Times (The Java™ Tutorials > Internationalization > Formatting)
https://docs.oracle.com/javase/tutorial/datetime/iso/format.html
1. Try to create a Calendar instance from the string - deal with any exceptions
2. Modify the resulting calendar value (add a day if you want) - deal with any exceptions
3. Convert the result back to a string if you want.
"2016"+"12"+ "convert to integer (13) + 1""2016"+"12"+ "convert to integer (31) + 1""2017"+"02"+ "convert to integer (28) + 1"
And what do you expect to happen for the second two examples above? You will get a string like '20161232' or '20170229' - those are valid strings. But they are NOT valid as dates.
-
If you have a valid date, you can also convert the String to a Date, then use DateFormat which will output a String.
-
Thank you guys.....that was very much helpful. Here is my code .
Is there any improvement can be done to this ?
import java.text.*;
import java.util.*;
public class HelloWorld {
public static void main(String[] args)throws Exception {
String dateStr = "13/12/2016";
SimpleDateFormat curFormater = new SimpleDateFormat("dd/MM/yyyy");
Date dateObj = curFormater.parse(dateStr);
Calendar calendar = Calendar.getInstance();
calendar .setTime(dateObj);
calendar.add(Calendar.DATE, 1);
String dt = curFormater .format(calendar.getTime());
System.out.println(dt);
SimpleDateFormat format1 = new SimpleDateFormat("yyyyMMdd");
String formatted = format1.format(calendar.getTime());
System.out.println(formatted);
}
}
-
Looks like you are a fast learner.
It's a shame that more people seem to think that reading the documentation, or learning from tutorials, is beneath them.
That is really the BEST, and FASTEST way to learn the basics. Especially when each of those has examples with code that actually works.
I've been doing Oracle DBA and programming work for 30+ years. You never stop learning. And the more expert a person is the more they hit the books. And they are usually the FIRST one to hit the books when a new release (e.g. Java or Oracle DB) comes out.
Save that example code in your 'toolkit'. Keep adding to that toolkit as you write more, simple modules.
-
Thanks a lot for your kind guiding. It's really great to know that you have been in programming field for 30+ years. But the help you are doing to the forthcoming programmers and developers equals a ton. . I was enjoying getting your help. I wish your help continues all time. Wishing you all the best.@rp0428
-
Could you please tell me is there any specific meaning to "Save that example code in your 'toolkit'. " ? What is the toolkit means here ?