Forum Stats

  • 3,836,963 Users
  • 2,262,207 Discussions
  • 7,900,157 Comments

Discussions

making a new string from old string

user575089
user575089 Member Posts: 466
edited Jan 17, 2017 2:22AM in Java Programming

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 ?

rpc1user575089eudriscabrera-JavaNetmorgalrMohammed Sardar

Answers

  • Unknown
    edited Dec 13, 2016 12:27PM
    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.

    rpc1user575089user575089
  • morgalr
    morgalr Member Posts: 457
    edited Dec 13, 2016 2:48PM

    If you have a valid date, you can also convert the String to a Date, then use DateFormat which will output a String.

  • user575089
    user575089 Member Posts: 466
    edited Dec 13, 2016 10:45PM

    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);

      }

    }

  • Unknown
    edited Dec 13, 2016 11:14PM

    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.

    eudriscabrera-JavaNetmorgalrMohammed Sardar
  • Mohammed Sardar
    Mohammed Sardar Member Posts: 190
    edited Jan 17, 2017 2:09AM

    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

  • Mohammed Sardar
    Mohammed Sardar Member Posts: 190
    edited Jan 17, 2017 2:22AM

    Could you please tell me is there any specific meaning to "Save that example code in your 'toolkit'. "         ? What is the toolkit means here ?

This discussion has been closed.