Posts

Showing posts from February, 2022

4 ways to add New Line Character to a String in Java

Image
When you are required to output Strings with your Java code, more often than not, there is a requirement to format the output correctly.  Adding a newline character between two strings in order to separate them, or create a paragraph is one of the most common requirements. Let's look at a few ways today, on how to output a newline between two Strings. 1. System.out.println inbuilt function One of the easiest ways, is to use the function System.out.println(). The println() function will automatically add a new line to the output, if using two strings.  Sample: String s1 = "This is a String." ; String s2 = "This is another String." ; System . out . println ( s1 ) ; System . out . println ( s2 ) ; If you don't want to use a separate System.out.println for every String, there are other ways as well. Read on! 2. Add a newline character "\n" You can simply append newline character yourself to the strings, and use a single System.out.print command

Find Day of the week from any Calendar date using Java code

Image
  If we have a date, and need to find which day of the week it was instantly, that would a really useful function, isn't it? This is also a useful function which can be used on a webpage having Calendar logic, or a Delivery website which wants to show which is the day of the week on which your order will be delivered.  Java has a very useful inbuilt function which we can use to get this functionality, and we'll show you how! For utilizing the functionality, you have to import a Java library:  import java . time . LocalDate ; Once you have the library, the code is really easy. Let's write a small working function. /**   * Function to return Day of the week   * @author computengine.com   * @param month   * @param day   * @param year   * @return  */ public static String findDay(int month, int day, int year) { LocalDate ldt = LocalDate . of ( year , month , day ) ; return String . valueOf ( ldt . getDayOfWeek ( ) ) ; } That's it! Isn't