karthikeya Boyini

karthikeya Boyini

1,421 Articles Published

Articles by karthikeya Boyini

Page 78 of 143

Reverse words in a given String in Java

karthikeya Boyini
karthikeya Boyini
Updated on 11-Mar-2026 5K+ Views

The order of the words in a string can be reversed and the string displayed with the words in reverse order. An example of this is given as follows.String = I love mangoes Reversed string = mangoes love IA program that demonstrates this is given as follows.Exampleimport java.util.regex.Pattern; public class Example {    public static void main(String[] args) {       String str = "the sky is blue";       Pattern p = Pattern.compile("\s");       System.out.println("The original string is: " + str);       String[] temp = p.split(str);       String rev = ""; ...

Read More

Decrement a Month using the Calendar Class in Java

karthikeya Boyini
karthikeya Boyini
Updated on 11-Mar-2026 268 Views

Import the following package for Calendar class in Javaimport java.util.Calendar;Firstly, create a Calendar object and display the current dateCalendar calendar = Calendar.getInstance(); System.out.println("Current Date = " + calendar.getTime());Now, let us decrement the month using the add() method and Calendar.MONTH constant. Set a negative value here since we are decrementingcalendar.add(Calendar.MONTH, -2);The following is an exampleExampleimport java.util.Calendar; public class Demo {    public static void main(String[] args) {       Calendar calendar = Calendar.getInstance();       System.out.println("Current Date = " + calendar.getTime());       // Decrementing Month by 2       calendar.add(Calendar.MONTH, -2);       System.out.println("Updated Date ...

Read More

Sort the words in lexicographical order in Java

karthikeya Boyini
karthikeya Boyini
Updated on 11-Mar-2026 2K+ Views

The words are sorted in lexicographical order or dictionary order. This means that the words are alphabetically ordered based on their component alphabets. An example of this is given as follows.The original order of the words is Tom Anne Sally John The lexicographical order of the words is Anne John Sally TomA program that demonstrates this is given as follows.Examplepublic class Example {    public static void main(String[] args) {       String[] words = { "Peach", "Orange", "Mango", "Cherry", "Apple" };       int n = 5;       System.out.println("The original order of the words is: ...

Read More

Subtract seconds from current time using Calendar.add() method in Java

karthikeya Boyini
karthikeya Boyini
Updated on 11-Mar-2026 721 Views

Import the following package for Calendar class in Javaimport java.util.Calendar;Firstly, create a Calendar object and display the current date and timeCalendar calendar = Calendar.getInstance(); System.out.println("Current Date and Time = " + calendar.getTime());Now, let us decrement the seconds using the calendar.add() method and Calendar.SECOND constant. Set a negative value since we are decrementingcalendar.add(Calendar.SECOND, -20);The following is an exampleExampleimport java.util.Calendar; public class Demo {    public static void main(String[] args) {       Calendar calendar = Calendar.getInstance();       System.out.println("Current Date = " + calendar.getTime());       // Subtract 20 seconds from current date       calendar.add(Calendar.SECOND, -20); ...

Read More

Unix date format in Java

karthikeya Boyini
karthikeya Boyini
Updated on 11-Mar-2026 564 Views

Use the ‘c’ date conversion character to display UNIX date format in Java.System.out.printf("Unix date format: %tc",d);Above, d is a date object.Date d = new Date();Exampleimport java.util.Date; import java.text.DateFormat; import java.text.SimpleDateFormat; public class Demo {    public static void main(String[] args) throws Exception {       Date d = new Date();       System.out.printf("Unix date format: %tc",d);       System.out.printf("Unix date format: %Tc",d);    } }OutputUnix date format: Mon Nov 26 12:24:10 UTC 2018 Unix date format: MON NOV 26 12:24:10 UTC 2018

Read More

Display numbers with thousands separator in Java

karthikeya Boyini
karthikeya Boyini
Updated on 11-Mar-2026 6K+ Views

To display number with thousands separator, set a comma flag.System.out.printf( "%,d",78567);The above would result.78, 567Let’s check for bigger numbers.System.out.printf( "%,d", 463758);The above would result.463,758Examplepublic class Demo {    public static void main( String args[] ) {       System.out.printf( "%,d", 95647 );       System.out.printf( "%,d", 687467 );       System.out.printf( "%,.2f", 7546.21 );       System.out.printf( "%,.2f", 463758.787 );       System.out.printf( "%,.2f", 123456.5 );    } }Output95,647 687,467 7,546.21 463,758.79 123,456.50

Read More

Locale-specific morning/afternoon indicator in Java

karthikeya Boyini
karthikeya Boyini
Updated on 11-Mar-2026 153 Views

Locale-specific morning/afternoon indicator is the AM/PM marker indicator.Use the ‘p’ conversion character to display AM/PM.System.out.printf("Morning/afternoon indicator: %tp",d);Exampleimport java.util.Date; public class Demo {    public static void main(String[] args) {       Date d = new Date();       System.out.printf("Morning/afternoon indicator: %tp",d);       System.out.printf("Morning/afternoon indicator: %Tp",d);    } }OutputMorning/afternoon indicator: pm Morning/afternoon indicator: PM

Read More

Permutation and Combination in Java

karthikeya Boyini
karthikeya Boyini
Updated on 11-Mar-2026 6K+ Views

Permutation and Combination are a part of Combinatorics. Permutation is the different arrangements that a set of elements can make if the elements are taken one at a time, some at a time or all at a time. Combination is is the different ways of selecting elements if the elements are taken one at a time, some at a time or all at a time.An example of this is given as follows −Permutation = factorial(n) / factorial(n-r); Combination = factorial(n) / (factorial(r) * factorial(n-r)); n = 5 r = 3 Permutation = 60 Combination = 10A program that demonstrates this ...

Read More

Argument Index in Java

karthikeya Boyini
karthikeya Boyini
Updated on 11-Mar-2026 667 Views

Argument indices allow programmers to reorder the output. Let us see an example.Examplepublic class Demo {    public static void main(String[] args) {       System.out.printf("Before reordering = %s %s %s %s %s %s", "one", "two", "three", "four", "five", "six" );       System.out.printf("After reordering = %6$s %5$s %4$s %3$s %2$s %1$s", "one", "two", "three", "four", "five", "six" );       System.out.printf("Before reordering = %d %d %d", 100, 200, 300);       System.out.printf("After reordering = %2$d %3$d %1$d", 100, 200, 300);    } }OutputBefore reordering = one two three four five six After reordering = six ...

Read More

Days till End of Year in Java

karthikeya Boyini
karthikeya Boyini
Updated on 11-Mar-2026 602 Views

To get the days until the end of the year, find the difference between the total days in the current year with the total number of passed.Firstly, calculate the day of the year.Calendar calOne = Calendar.getInstance(); int dayOfYear = calOne.get(Calendar.DAY_OF_YEAR);Now, calculate the total days in the current year 2018.int year = calOne.get(Calendar.YEAR); Calendar calTwo = new GregorianCalendar(year, 11, 31); int day = calTwo.get(Calendar.DAY_OF_YEAR); System.out.println("Days in current year: "+day);Find the difference and you will get the days until the end of the year.The following is the complete example.Exampleimport java.util.Calendar; import java.util.GregorianCalendar; public class Demo {    public static void main(String args[]) ...

Read More
Showing 771–780 of 1,421 articles
« Prev 1 76 77 78 79 80 143 Next »
Advertisements