import java.lang.reflect.Method; try { String myClassName = "tr.com.xx.xx.xx.Example"; String myMethodName = "exampleMethod"; Class<?> myClassRef = Class.forName(myClassName); Object instanceOfMyClass = myClassRef.newInstance(); //my object input types in order: param1(int), param2(String), param3(MyDTO), param4(List<MyDTO>) Method method = myClassRef.getDeclaredMethod(myMethodName, int.class, String.class, MyDTO.class, List.class); MyResult respoonse = (MyResult) method.invoke(instanceOfMyClass, param1, param2, param3, param4); System.out.println("response" + response.getX()); } catch(Exception e) { //logging System.out.println(e.getMessage());
Showing posts with label java basics. Show all posts
Showing posts with label java basics. Show all posts
Tuesday
Dynamically calling a class - method in java by using reflection
Dynamically call a method on a dynamically given class by using reflection:
Why is SimpleDateFormat changing given date?
//The upper case Y is "week year". It has 364 or 371 days //The lower case y has usual 365 or 366. //So they give different outputs: //Locale.forLanguageTag("tr_TR") >> comes with Java 7 import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Locale; public class Test { public static void main(String[] args) { try { String dateString = new java.util.Date().toString(); System.out.println(dateString); SimpleDateFormat format = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy", Locale.forLanguageTag("tr_TR")); Date date = format.parse(dateString); System.out.println(date.toString()); SimpleDateFormat format2 = new SimpleDateFormat("EEE MMM dd HH:mm:ss z YYYY", Locale.forLanguageTag("tr_TR")); Date date2 = format2.parse(dateString); System.out.println(date2.toString()); } catch (ParseException e) { //log } } }
Output: Mon May 14 10:31:14 EEST 2018 Mon May 14 10:31:14 EEST 2018 Mon Jan 01 09:31:14 EET 2018
Friday
Sort list of strings by using Localization
import java.text.Collator; List<String> yourValues = …; Collections.sort(yourValues, Collator.getInstance(Locale.forLanguageTag("your locale ")));
See Collator interface.
Join ArrayList String elements into a single String with a separator
Download commons-lang3-3.0.1.jar.
https://mvnrepository.com/artifact/
org.apache.commons/commons-lang3/3.0.1
|
For maven:
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.0.1</version>
</dependency>
|
import
java.util.ArrayList;
import java.util.List;
import
org.apache.commons.lang3.StringUtils;
public class Test {
public static void main(String[]
args) {
List<String> list = new
ArrayList<String>();
list.add("less");
list.add("talk");
list.add("more");
list.add("code");
System.out.println(StringUtils.join(list,
",
"));
}
}
|
OUTPUT:
less, talk, more,
code
|
Tuesday
Get URL content
import
java.io.BufferedReader;
import
java.io.BufferedWriter;
import java.io.File;
import
java.io.FileWriter;
import
java.io.IOException;
import
java.io.InputStream;
import
java.io.InputStreamReader;
import
java.net.MalformedURLException;
import java.net.URL;
import
java.net.HttpURLConnection;
public class Test {
public static void main(String[]
args) {
BufferedWriter bw = null;
BufferedReader br = null;
String inputLine;
try {
//If you are
behind a proxy you should set the following settings.
//You can look my
other post to learn your own proxy infos
// System.setProperty("http.proxyHost",
"your proxy host");
// System.setProperty("http.proxyPort",
"your proxy port");
URL url = new URL("http://lesstalkmorecode.blogspot.com.tr/");
HttpURLConnection conn =
(HttpURLConnection) url.openConnection();
conn.setReadTimeout(10*1000);
conn.setConnectTimeout(5*1000);
InputStream in =
conn.getInputStream();
br = new BufferedReader(new
InputStreamReader(in));
String fileName = "/users/content.html";
File file = new File(fileName);
if (!file.exists())
file.createNewFile();
FileWriter fw = new
FileWriter(file.getAbsoluteFile());
bw = new
BufferedWriter(fw);
while ((inputLine =
br.readLine()) != null)
bw.write(inputLine);
} catch
(MalformedURLException e) {
//log
} catch (IOException e) {
//log
} finally {
try {
if(bw != null)
bw.close();
if(br != null)
br.close();
} catch (IOException e) {
//log
}
}
}
}
|
View your proxy settings
- Open Google Chrome enter below address in the bar:
- chrome://net-internals/#proxy
- Open cmd, write >> ipconfig /all
- See >> Windows IP Configuration >> Primary Dns Suffix
- Open Internet Explorer
- Internet Options >> Connections >> LAN Settings
- See from the pop-up address bar
- Open Mozilla Firefox enter below address in the bar:
- about:preferences#advanced
Friday
Check if a web service is accessible? Ping a host for availability.
This summary is not available. Please
click here to view the post.
Subscribe to:
Posts (Atom)