Programming Articles - Page 1442 of 3366

How to format year using SimpleDateFormat in Java?

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

204 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

473 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

902 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

300 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

How to check if a variable contains number greater than 1 in an R data frame?

Nizamuddin Siddiqui
Updated on 05-Feb-2021 10:55:19

1K+ Views

The variables in an R data frame are referred to as the columns of the data frame. Sometimes we have a threshold value for a particular column and we need to check whether all the values in that column are greater than or less than the threshold. For this purpose, we can make use of ifelse function as shown in the below examples.Example1 Live DemoConsider the below data frame −set.seed(24) x

How to create a random sample of week days in R?

Nizamuddin Siddiqui
Updated on 05-Feb-2021 10:52:32

437 Views

To create a vector of weekdays we can use the command weekdays(Sys.Date()+0:6) and if we want to create a random sample of week days then sample function can be used along with the weekdays command. For example, if we want to create a random sample of 20 days then it can be done as sample(weekdays(Sys.Date()+0:6),20,replace=TRUE).Examples Live DemoExample1

Advertisements