Found 7442 Articles for Java

How to parse Date from String in the format: dd/MM/yyyy to dd/MM/yyyy in java?

Maruthi Krishna
Updated on 14-Oct-2019 07:46:50

15K+ Views

The java.text package provides a class named SimpleDateFormat which is used to format and parse dates in required manner (local).One of the constructors of this class accepts a String value representing the desired date format and constructors SimpleDateFormat object.The format() method of this class accepts a java.util.Date object and returns a date/time string in the format represented by the current object.Therefore, to parse a date String to another date format −Get the input date string.Convert it into java.util.Date object.Instantiate the SimpleDateFormat class by passing the desired (new) format as string to its constructor.Invoke the format() method by passing the above ... Read More

Is it possible to check if a String only contains ASCII in java?

Maruthi Krishna
Updated on 14-Oct-2019 07:37:37

2K+ Views

Using regular expressionYou can find whether a particular String value contains ASCII characters using the following regular expression −\A\p{ASCII}*\zThe matches() method of the String class accepts a regular expression and verifies whether the current string matches the given expression if so, it returns true, else it returns false.Therefore, Invoke the matches() method on the input/required string by passing the above specified regular expression as a parameter.Example Live Demoimport java.util.Scanner; public class OnlyASCII {    public static void main(String[] args) {       Scanner sc = new Scanner(System.in);       System.out.println("Enter a string value: ");       String input ... Read More

How do we initialize an array within object parameters in java?

Maruthi Krishna
Updated on 14-Oct-2019 07:33:00

5K+ Views

You can initialize the array variable which is declared inside the class just like any other value, either using constructor or, using the setter method.ExampleIn the following Java example, we are declaring an instance variable of array type and initializing it from the constructor. Live Demopublic class Student {    String name;    int age;    String subs[];    Student(String name, int age, String subs[]){    this.name = name;    this.age = age;    this.subs = subs; } public void display() {    System.out.println("Name: "+this.name);    System.out.println("Age :"+this.age);    System.out.print("Subjects: ");    for(int i = 0; i < subs.length; i++) { ... Read More

How to get ArrayList<String> to ArrayList<Object> and vice versa in java?

Maruthi Krishna
Updated on 07-Jun-2024 10:28:55

949 Views

ArrayList to ArrayList Instead of the typed parameter in generics (T) you can also use “?”, representing an unknown type. These are known as wild cards you can use a wild card as − Type of parameter or, a Field or, a Local field. Using wild cards, you can convert ArrayList to ArrayList as − ArrayList stringList = (ArrayList)(ArrayList)(list); Example import java.util.ArrayList; import java.util.Iterator; import java.util.ListIterator; public class ArrayListExample { public static void main(String args[]) { //Instantiating an ArrayList object ... Read More

How can we convert a JSON array to a list using Jackson in Java?

Aishwarya Naglot
Updated on 05-Jun-2025 14:58:10

9K+ Views

Jackson is a Java-based library, and it can be useful for converting Java objects to JSON and JSON to Java objects. A Jackson API is faster than other API, needs less memory, and is good for large objects. We can convert a JSON array to a list using the ObjectMapper class. It has a useful method,  readValue(), which takes a JSON string and converts it to the object class specified in the second argument. Before proceeding further, first of all, we need to import the Jackson library into our project. To do this, you can add the following dependency to ... Read More

How can we serialize a list of objects using flexjson in Java?

raja
Updated on 06-Jul-2020 12:26:47

1K+ Views

The Flexjson is a lightweight library for serializing and deserializing Java objects into and from JSON format. We can serialize a list of objects using the serialize() method of JSONSerializer class. This method can perform a shallow serialization of the target instance. We need to pass a list of objects of List type as an argument to the serialize() method.Syntaxpublic String serialize(Object target)Exampleimport flexjson.JSONSerializer; import java.util.*; public class JsonSerializeListTest {    public static void main(String[] args) {       JSONSerializer serializer = new JSONSerializer().prettyPrint(true); // pretty print JSON       Student s1 = new Student("Raja", "Ramesh", 28, "Hyderabad");       Student s2 = new Student("Suresh", "Kumar", 30, "Chennai");   ... Read More

How can we create a JSON using JsonGenerator in Java?

Aishwarya Naglot
Updated on 13-May-2025 15:43:25

7K+ Views

JSON is a format for storing and exchanging data. It is easily readable and also easy to parse. Creating JSON Using Java JsonGenerator In Java, we can create a JSON object using the JsonGenerator class. JsonGenerator is class in the javax.json.stream package that is used to create JSON data. It provides methods to write JSON objects, arrays, and values. To use the JsonGenerator class, we need to add the javax.json library to our project. If you are using Maven, add this to your pom.xml file: javax.json javax.json-api 1.1.4 If you are not using Maven, you can download ... Read More

How to create a JSON using Jackson Tree Model in Java?

Aishwarya Naglot
Updated on 13-May-2025 15:39:15

1K+ Views

To create a JSON using the Jackson Tree Model in Java, we can use the ObjectMapper class. The ObjectMapper class is part of the Jackson library and is used to convert Java objects to JSON and vice versa. To use the Jackson library, we need to add it to our project. If you are using Maven, add this to your pom.xml file: com.fasterxml.jackson.core jackson-databind 2.13.0 If you are not using Maven, you can download the jar file from here. Creating a JSON using Jackson Tree Model in ... Read More

How do we make my string comparison case insensitive in java?

Maruthi Krishna
Updated on 11-Oct-2019 08:25:47

827 Views

We can compare Strings in Java in various ways −Using the comapareTo() method − The compareTo() method compares two strings lexicographically. The comparison is based on the Unicode value of each character in the strings. The character sequence represented by this String object is compared lexicographically to the character sequence represented by the argument string.Example Live Demoimport java.util.Scanner; public class StringComparison {    public static void main(String[] args) {       Scanner sc = new Scanner(System.in);       System.out.println("Enter string1: ");       String str1 = sc.next();       System.out.println("Enter string2: ");       String str2 ... Read More

How to parse for words in a string for a specific word in java?

Maruthi Krishna
Updated on 05-Jun-2025 15:21:38

978 Views

In this article, we will learn how to parse for words in a string for a specific word in Java,  i.e., we need to check if a specific word exists in a string, and if it does, we will parse the string to find that word. There are various methods in Java that you can use to parse a string for a specific word. Here we are going to discuss 3 of them. Using the contains() method Using the indexOf() method Using the StringTokenizer class Using ... Read More

Advertisements