Found 2616 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

13K+ 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

1K+ 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

4K+ 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

707 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?

raja
Updated on 06-Jul-2020 12:30:14

8K+ Views

A Jackson is a Java-based library and it can be useful to convert Java objects to JSON and JSON to Java Object. A Jackson API is faster than other API, needs less memory area and is good for the 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.Exampleimport java.util.*; import com.fasterxml.jackson.databind.*; public class JSONArrayToListTest1 {    public static void main(String args[]) {       String jsonStr = "[\"INDIA\", \"AUSTRALIA\", \"ENGLAND\", \"SOUTH AFRICA\", \"WEST ... 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?

raja
Updated on 06-Jul-2020 12:19:03

5K+ Views

The JsonGenerator is a base class that defines public API for writing JSON content. The instances are created using factory methods of a JsonFactory instance. Once we can get the JsonGenerator from factory instance then use the writeStartObject() for writing starting marker of a JSON Object value,  writeFieldName() method for writing a field name, writeString() method for outputting a String value, writeStartArray() method for writing starting marker of a Array value and writeEndObject() method for writing closing marker of a JSON Object value.Syntaxpublic abstract class JsonGenerator extends Object implements Closeable, Flushable, VersionedExampleimport java.io.*; import com.fasterxml.jackson.core.*; public class JsonGeneratorTest {      public static void main(String ... Read More

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

raja
Updated on 06-Jul-2020 12:20:51

959 Views

In the Jackson library, we can use the Tree Model to represent the JSON structure and perform the CRUD operations via JsonNode. This Jackson Tree Model is useful, especially in cases where a JSON structure does not map to Java classes. We can create a JSON in the Jackson library using the JsonNodeFactory, it can specify the methods for getting access to Node instances as well as the basic implementation of the methods. We can use the set() and put() methods of ObjectNode class to populate the data.Syntaxpublic class JsonNodeFactory extends Object implements SerializableExampleimport java.io.*; import com.fasterxml.jackson.databind.*; import com.fasterxml.jackson.databind.node.*; public class JacksonTreeModelTest {    public static ... Read More

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

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

656 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 11-Oct-2019 08:30:04

617 Views

There are various methods in Java using which you can parse for words in a string for a specific word. Here we are going to discuss 3 of them.The contains() methodThe contains() method of the String class accepts a sequence of characters value and verifies whether it exists in the current String. If found it returns true else, it returns false.Example Live Demoimport java.util.StringTokenizer; import java.util.regex.Pattern; public class ParsingForSpecificWord {    public static void main(String args[]) {       String str1 = "Hello how are you, welcome to Tutorialspoint";       String str2 = "Tutorialspoint";       if ... Read More

Advertisements