Found 33676 Articles for Programming

How to use String.format() in Java?

Maruthi Krishna
Updated on 06-Feb-2021 03:59:38

178 Views

The format() method of the java.lang.String class accepts a format string and an array of arguments and the string in specified format.ExampleFollowing example formats a date using the format() method −Live Demoimport java.util.Calendar; import java.util.GregorianCalendar; public class Test {    public static void main(String args[]){           //Instantiating the GregorianCalendar         Calendar cal = GregorianCalendar.getInstance();       System.out.println("Date: "+cal.get(Calendar.DATE));       System.out.println("Month: "+cal.get(Calendar.MONTH));       System.out.println("Year: "+cal.get(Calendar.YEAR));           Object arr[] = { "Date", cal };       System.out.println("Desired Format: ");       System.out.println(String.format("%1$s = %2$tY ... Read More

What are SimpleDateFormat Format Codes in Java?

Maruthi Krishna
Updated on 06-Feb-2021 03:55:45

295 Views

The java.text.SimpleDateFormat class is used to format and parse a string to date and date to string.Parsing a date stringOne of the constructors of this class accepts a String value representing the desired date format and creates SimpleDateFormat object. To parse/convert a string as a Date objectInstantiate this class by passing desired format string.Parse the date string using the parse() method.Following is the list of letters for formatting with their descriptions and examples −LetterComponentExampleGEra designatorAD, BCyYear2005, 96YWeek year2005, 96MMonth in yearSeptember, Sep, 09LMonth in yearSeptember, Sep, 09wWeek in year23WWeek in month3DDay in year129dDay in month27FDay of week in month5EDay in a ... Read More

How to format year using SimpleDateFormat in Java?

Maruthi Krishna
Updated on 06-Feb-2021 03:55:23

191 Views

The java.text.SimpleDateFormat class is used to format and parse a string to date and date to string. To parse a date string −Instantiate this class by passing desired format string.Parse the date string using the parse() method.ExampleLive Demoimport java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; public class Sample {    public static void main(String args[]) throws ParseException {         SimpleDateFormat formatter1 = new SimpleDateFormat("HH:mm:ss");             Date time1 = formatter1.parse("07:25:30");       System.out.println("Date value: "+time1);       SimpleDateFormat formatter3 = new SimpleDateFormat("hh 'o''clock' a");             Date time3 = formatter3.parse("09 ... Read More

What are various ways to compare dates in Java?

Maruthi Krishna
Updated on 06-Feb-2021 03:49:47

462 Views

Using the LocalDate classThe java.time.LocalDate class represents the local date i.e. the date without time zone, you can use this object instead of Date. This class provides various methods such as isBefore(), isAfter() and, isEqual() to compare two dates −ExampleLive Demoimport java.time.LocalDate; public class Sample {    public static void main(String args[]) {         LocalDate date1 = LocalDate.of(2007, 11, 25);       LocalDate date2 = LocalDate.of(1999, 9, 12);             Boolean bool1 = date1.isAfter(date2);         Boolean bool2 = date1.isBefore(date2);       Boolean bool3 = date1.isEqual(date2);       if(bool1){ ... Read More

How to compare two dates in String format in Java?

Maruthi Krishna
Updated on 06-Feb-2021 03:49:19

7K+ Views

The java.text.SimpleDateFormat class is used to format and parse a string to date and date to string.One of the constructors of this class accepts a String value representing the desired date format and creates SimpleDateFormat object. To parse/convert a string as a Date object Instantiate this class by passing desired format string.Parse the date string using the parse() method.The util.Date class represents a specific instant time This class provides various methods such as before(), after() and, equals() to compare two datesExampleOnce you create date objects from strings you can compare them using either of these methods as shown below −Live Demoimport java.text.ParseException; ... Read More

What are various ways to compare time values in Java?

Maruthi Krishna
Updated on 06-Feb-2021 03:48:47

2K+ Views

The LocalTime class represents the local time i.e. the time without time zone. This class provides various methods such as isBefore(), isAfter() and, isEqual() to compare two times.ExampleLive Demoimport java.time.LocalTime; public class Test {    public static void main(String args[]) {         LocalTime Time1 = LocalTime.of(10, 15, 45);       LocalTime Time2 = LocalTime.of(07, 25, 55);             Boolean bool1 = Time1.isAfter(Time2);         Boolean bool2 = Time1.isBefore(Time2);       if(bool1){          System.out.println(Time1+" is after "+Time2);       }else if(bool2){          System.out.println(Time1+" ... Read More

How to compare two dates along with time in Java?

Maruthi Krishna
Updated on 06-Feb-2021 03:47:06

894 Views

The java.time.LocalDateTime class represents the local date and time i.e. the date without time zone, you can use this object instead of Date. This class provides various methods such as isBefore(), isAfter() and, isEqual() to compare two dates −ExampleLive Demoimport java.time.LocalDateTime; public class Test {    public static void main(String args[]) {         LocalDateTime dateTime1 = LocalDateTime.of(2007, 11, 25, 10, 15, 45);       LocalDateTime dateTime2 = LocalDateTime.of(1999, 9, 12, 07, 25, 55);             Boolean bool1 = dateTime1.isAfter(dateTime2);         Boolean bool2 = dateTime1.isBefore(dateTime2);       Boolean bool3 = ... Read More

How to Format a Date String using SimpleDateFormat in Java?

Maruthi Krishna
Updated on 06-Feb-2021 03:45:39

294 Views

One of the constructors of this class accepts a String value representing the desired date format and creates SimpleDateFormat class. To parse/convert a string as a Date object −Instantiate this class by passing desired format string.Parse the date string using the parse() method.ExampleLive Demoimport java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; public class Sample {    public static void main(String args[]) throws ParseException {         String date_string = "2007-25-06";       //Instantiating the SimpleDateFormat class       SimpleDateFormat formatter = new SimpleDateFormat("yyyy-dd-MM");             //Parsing the given String to Date object       Date ... Read More

How to create Date object from String value in Java?

Maruthi Krishna
Updated on 06-Feb-2021 03:45:19

2K+ Views

Using the SimpleDateFormat classOne of the constructors of this class accepts a String value representing the desired date format and creates SimpleDateFormat class. To parse/convert a string as a Date object −Instantiate this class by passing desired format string.Parse the date string using the parse() method.ExampleLive Demoimport java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; public class Sample {    public static void main(String args[]) throws ParseException {         String date_string = "2007-25-06";       //Instantiating the SimpleDateFormat class       SimpleDateFormat formatter = new SimpleDateFormat("yyyy-dd-MM");             //Parsing the given String to Date object   ... Read More

How to change the name of a data frame in R?

Nizamuddin Siddiqui
Updated on 05-Feb-2021 10:57:44

9K+ Views

To change the name of a data frame, we can set the original name to the new name. Now both of the names can be used. Most of the times the purpose behind changing the name of the data frame is that, the original name does not seem to be a valid name based on the characteristics of the data. For example, if we have normally distributed columns in the data frame then we can name it as normal_distribution. This will help everyone to understand the data belongs to normal distribution.Example1 Live Demoset.seed(24) x

Advertisements