Wednesday, 26 August 2020

Convert array to array list and and get impact

 import java.util.ArrayList;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
class Main {
public static void main(String[] args) {
String[] array = {"a", "b", "c", "d", "e"};

//Method 1
// List<String> list = Arrays.asList(array);
// list.add("p");
List<String> list1 = new ArrayList<String>();
Collections.addAll(list1, array);
list1.add("p");
System.out.println(list1);
//System.out.println(list);
System.out.println("Hello world!");
}
}

Thursday, 13 August 2020

timezone wise get expired company list

 ----------------------------------------------------------------------------------------------------------

SELECT 

    sh_company.company_id,

    sh_timezone.tz_name,

    sh_timezone.tz_id,

    sh_timezone.tz_unit_hm,

    CONVERT_TZ(sh_company.paywall_end_date,

            sh_timezone.tz_name,

            sh_timezone.tz_name) AS time,

    CONVERT_TZ(sh_company.paywall_end_date,

            'utc',

            sh_timezone.tz_name) AS time1,

    sh_company.paywall_end_date

FROM

    sh_company

        LEFT JOIN

    sh_location ON sh_location.company_id = sh_company.company_id

        LEFT JOIN

    sh_timezone ON sh_timezone.tz_id = sh_location.tz_id

WHERE

    paywall_status = 'INACTIVE'

        AND CONVERT_TZ(paywall_end_date,

            sh_timezone.tz_name,

            sh_timezone.tz_name) <= CONVERT_TZ('2020-08-14 23:59:59',

            'utc',

            sh_timezone.tz_name)

        AND paywall_end_date != '0000-00-00 00:00:00'

        AND name != '1Huddle'

        AND sh_location.head_location = 1;

-------------------------------------------------------------------------------------------------------------

Thursday, 25 June 2020

CSV read and write using apache common csv

https://www.callicoder.com/java-read-write-csv-file-apache-commons-csv/

Reading a CSV file (Access Values by Names assigned to each column)

In the earlier example, We accessed the values in each record using their column index. If you don’t want to use column indexes to retrieve the values in each record, then you can assign names to each column in the CSV file and retrieve the values using the assigned names.
Check out the following example where we define a manual header and retrieve the values using the header names.
import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVParser;
import org.apache.commons.csv.CSVRecord;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;
import java.nio.file.Files;
import java.nio.file.Paths;

public class CSVReaderWithManualHeader {
    private static final String SAMPLE_CSV_FILE_PATH = "./users.csv";

    public static void main(String[] args) throws IOException {
        try (
            Reader reader = Files.newBufferedReader(Paths.get(SAMPLE_CSV_FILE_PATH));
            CSVParser csvParser = new CSVParser(reader, CSVFormat.DEFAULT
                    .withHeader("Name", "Email", "Phone", "Country")
                    .withIgnoreHeaderCase()
                    .withTrim());
        ) {
            for (CSVRecord csvRecord : csvParser) {
                // Accessing values by the names assigned to each column
                String name = csvRecord.get("Name");
                String email = csvRecord.get("Email");
                String phone = csvRecord.get("Phone");
                String country = csvRecord.get("Country");

                System.out.println("Record No - " + csvRecord.getRecordNumber());
                System.out.println("---------------");
                System.out.println("Name : " + name);
                System.out.println("Email : " + email);
                System.out.println("Phone : " + phone);
                System.out.println("Country : " + country);
                System.out.println("---------------\n\n");
            }
        }
    }
}




Generating a CSV file

Finally, Let’s see an example of generating a CSV file with Apache Commons CSV.
import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVPrinter;

import java.io.*;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Arrays;

public class CSVWriter {
    private static final String SAMPLE_CSV_FILE = "./sample.csv";

    public static void main(String[] args) throws IOException {
        try (
            BufferedWriter writer = Files.newBufferedWriter(Paths.get(SAMPLE_CSV_FILE));

            CSVPrinter csvPrinter = new CSVPrinter(writer, CSVFormat.DEFAULT
                    .withHeader("ID", "Name", "Designation", "Company"));
        ) {
            csvPrinter.printRecord("1", "Sundar Pichai ♥", "CEO", "Google");
            csvPrinter.printRecord("2", "Satya Nadella", "CEO", "Microsoft");
            csvPrinter.printRecord("3", "Tim cook", "CEO", "Apple");

            csvPrinter.printRecord(Arrays.asList("4", "Mark Zuckerberg", "CEO", "Facebook"));

            csvPrinter.flush();            
        }
    }
}
The above program will generate the following CSV file -
ID,Name,Designation,Company
1,Sundar Pichai ♥,CEO,Google
2,Satya Nadella,CEO,Microsoft
3,Tim cook,CEO,Apple
4,Mark Zuckerberg,CEO,Facebook