Found 2617 Articles for Java

How to get the list of jpg files in a directory in Java?

Maruthi Krishna
Updated on 08-Feb-2021 11:06:23

924 Views

The String[] list(FilenameFilter filter) method of a File class returns a String array containing the names of all the files and directories in the path represented by the current (File) object. But the retuned array contains the filenames which are filtered based on the specified filter. The FilenameFilter is an interface in Java with a single method.accept(File dir, String name)To get the file names based on extensions implement this interface as such and pass its object to the above specified list() method of the file class.Assume we have a folder named ExampleDirectory in the directory D with 7 files and 2 directories ... Read More

How to create Directories using the File utility methods in Java?

Maruthi Krishna
Updated on 08-Feb-2021 11:05:09

299 Views

Since Java 7 the File.02s class was introduced this contains (static) methods that operate on files, directories, or other types of files.The createDirectory() method of the Files class accepts the path of the required directory and creates a new directory.ExampleFollowing Java example reads the path and name of the directory to be created, from the user, and creates it.Live Demoimport java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.util.Scanner; public class Test {    public static void main(String args[]) throws IOException {       System.out.println("Enter the path to create a directory: ");       Scanner sc = new Scanner(System.in);     ... Read More

How to convert primitive data into wrapper class using Java?

Maruthi Krishna
Updated on 06-Feb-2021 04:37:48

1K+ Views

Java provides certain classes called wrapper classes in the java.lang package. The objects of these classes wrap primitive datatypes within them.Using wrapper classes, you can also add primitive datatypes to various Collection objects such as ArrayList, HashMap etc. You can also pass primitive values over network using wrapper classes.ExampleLive Demoimport java.util.Scanner; public class WrapperExample {    public static void main(String args[]){       Scanner sc = new Scanner(System.in);       System.out.println("Enter an integer value: ");       int i = sc.nextInt();             //Wrapper class of an integer       Integer obj ... Read More

How to inherit multiple interfaces in Java?

Maruthi Krishna
Updated on 06-Feb-2021 04:34:58

337 Views

An interface in Java is similar to class but, it contains only abstract methods and fields which are final and static.Just like classes you can extend one interface from another using the extends keyword. You can also extend multiple interfaces from an interface using the extends keyword, by separating the interfaces using comma (, ) as −interface MyInterface extends ArithmeticCalculations, MathCalculations{ExampleFollowing is the Java program demonstrating, how to extend multiple interfaces from a single interface.interface ArithmeticCalculations{    public abstract int addition(int a, int b);    public abstract int subtraction(int a, int b); } interface MathCalculations {    public abstract double ... Read More

What is Java reg ex to check for date and time?

Maruthi Krishna
Updated on 06-Feb-2021 04:32:21

1K+ Views

To match a regular expression with the given string You need to:.Compile the regular expression of the compile() method of the Pattern class.Get the Matcher object bypassing the required input string as a parameter to the matcher() method of the Pattern class.The matches() method of the Matcher class returns true if a match occurs else it returns false. Therefore, invoke this method to validate the data.ExampleFollowing is a Java regular expression example matches only dateLive Demoimport java.util.ArrayList; import java.util.List; import java.util.regex.Matcher; import java.util.regex.Pattern; public class Sample {    public static void main(String args[]){       //Creating the list to ... Read More

How to get Time in Milliseconds for the Given date and time in Java?

Maruthi Krishna
Updated on 06-Feb-2021 04:31:49

1K+ 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.You can get the epoch time using the getTime() 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 {         //Instantiating the SimpleDateFormat class       SimpleDateFormat dateformatter = new ... Read More

How to get current date/time in milli seconds in Java?

Maruthi Krishna
Updated on 06-Feb-2021 04:29:11

6K+ Views

The getTime() method of the Date class retrieves and returns the epoch time (number of milliseconds from Jan 1st 1970 00:00:00 GMT0.ExampleLive Demoimport java.util.Date; public class Sample {    public static void main(String args[]){         //Instantiating the Date class       Date date = new Date();       long msec = date.getTime();       System.out.println("Milli Seconds: "+msec);    } }OutputMilli Seconds: 1605160094688The getTimeInMillis() method of the calendar class retrieves and returns the time in milli seconds from the epochepoch time (Jan 1st 1970 00:00:00 GMT).ExampleLive Demoimport java.util.Calendar; public class Sample {    public static ... Read More

How to get (format) date and time from mill seconds in Java?

Maruthi Krishna
Updated on 07-Sep-2021 13:12:20

2K+ 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 format milli seconds to date −Create the format string as dd MMM yyyy HH:mm:ss:SSS Z.The Date class constructor accepts a long value representing the milliseconds as a parameter and creates a date object.Finally format the date object using the format() method.Exampleimport java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; public class Sample { public static void main(String args[]) throws ParseException { ... Read More

What are date temporal fields in Java?

Maruthi Krishna
Updated on 06-Feb-2021 04:26:06

2K+ Views

A temporal field is a field of date-time, such as month-of-year or hour-of-minute. These fields are represented by the TemporalField interface and the ChronoField class implements this interface.Following are the list of various temporal fields regarding date supported by the ChronoField class −FieldDescriptionALIGNED_DAY_OF_WEEK_IN_MONTHThis field represents the day of the week with in a month.ALIGNED_DAY_OF_WEEK_IN_YEARThis field represents the aligned day of a week in an year.ALIGNED_WEEK_OF_MONTHThis field represents the aligned wee of a month.ALIGNED_WEEK_OF_YEARThis field represents the aligned week of an year.DAY_OF_MONTHThis field represents the day of a month.DAY_OF_WEEKThis field represents the day of a week.DAY_OF_YEARThis field represents the day of ... Read More

What are time temporal fields in Java?

Maruthi Krishna
Updated on 06-Feb-2021 04:22:39

661 Views

A temporal field is a field of date-time, such as month-of-year or hour-of-minute. These fields are represented by the TemporalField interface and the ChronoField class implements this interface.Following are the list of various temporal fields regarding time supported by the ChronoField class −FieldDescriptionCLOCK_HOUR_OF_AMPMThis field represents the clock hour of a day (am/pm).AMPM_OF_DAYThis field represents the ap/pm of the day.CLOCK_HOUR_OF_DAYThis field represents the clock hour of a day.HOUR_OF_AMPMThis field represents the hour of a day (am/pm).HOUR_OF_DAYThis field represents the hour of a day.INSTANT_SECONDSThis field represents the instant epoch seconds.MICRO_OF_DAYThis field represents the micro of a day.MICRO_OF_SECONDThis field represents the micro of ... Read More

Advertisements