Archive For The “Java” Category

Remove vietnammese mark in string

By |

Example: public class CharacterUtils {     private static char[] SOURCE_CHARACTERS = { ‘À’, ‘Á’, ‘Â’, ‘Ã’, ‘È’, ‘É’,             ‘Ê’, ‘Ì’, ‘Í’, ‘Ò’, ‘Ó’, ‘Ô’, ‘Õ’, ‘Ù’, ‘Ú’, ‘Ý’, ‘à’, ‘á’, ‘â’,             ‘ã’, ‘è’, ‘é’, ‘ê’, ‘ì’, ‘í’, ‘ò’, ‘ó’, ‘ô’, ‘õ’, ‘ù’, ‘ú’, ‘ý’,             ‘Ă’, ‘ă’,…

Read more »

Convert string to date in java

By |

try { Date date = new Date(); Calendar cal = Calendar.getInstance(); cal.set(2017, 04, 08); int startYear = cal.get(Calendar.YEAR); int startMonth = cal.get(Calendar.MONTH); int startDay = cal.get(Calendar.DAY_OF_MONTH); System.out.println(“date year ” + startYear); System.out.println(“date month ” + startMonth); System.out.println(“date day ” + startDay); cal.setTime(date); int year = cal.get(Calendar.YEAR); int month = cal.get(Calendar.MONTH); int day = cal.get(Calendar.DAY_OF_MONTH); System.out.println(“date…

Read more »

Date format Java

By |

Letter Date or Time Component Presentation Examples G Era designator Text AD y Year Year 1996; 96 Y Week year Year 2009; 09 M Month in year Month July; Jul; 07 w Week in year Number 27 W Week in month Number 2 D Day in year Number 189 d Day in month Number 10…

Read more »

Run cmd command line on windows by java

By |

ProcessBuilder builder = new ProcessBuilder(“cmd.exe”, “/c”, “adb kill-server && adb start-server && adb devices”); builder.redirectErrorStream(true); Process p = builder.start();

Read more »

Run TestNG class in main method

By |

public static void main(String[] args) { TestListenerAdapter testListenerAdapter = new TestListenerAdapter(); TestNG testng = new TestNG(); testng.setTestClasses(new Class[] { test.class }); testng.addListener(testListenerAdapter); testng.run(); }

Read more »

Getting the name of the current executing method – Java

By |

String name = new Object(){}.getClass().getEnclosingMethod().getName();

Read more »

Read csv file – Java

By |

String csvFile = “C:” + File.separator + “data.csv”; try { List<String> lines = Files.readAllLines(new File(csvFile).toPath()); dealDatas = new Object[lines.size()][9]; String[] datas = new String[9]; String line = “”; for (int i = 0; i < lines.size(); i++) { line = lines.get(i); datas = line.split(“,”); dealDatas[i] = datas; } } catch (Exception e) { e.printStackTrace(); }

Read more »

Kéo thả file jar vào file bat để mở file jar trong windows

By |

Tạo file bat 1. Tạo file start.bat 2. Mở file vừa tạo trong chế độ chỉnh sửa 3. Thêm 2 dòng lệnh java -jar %1 cmd.exe 4. Lưu và đóng file Test Copy file jar vào cùng thư mục với file bat Kéo thả file jar vào file bat (không cần mở file bat) File…

Read more »

Ghi, đọc text từ file txt

By |

package quan.tutorial.com; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileReader; import java.io.FileWriter; public class WriteReadTextToTXTFile { public static void main(String[] args) { try { File file = new File(“D://test.txt”); String content = “test\r\ntest\r\ntest\r\ntest\r\ntest\r\ntest\r\ntest\r\ntest\r\ntest\r\ntest\r\n”; //write FileWriter fileWriter = new FileWriter(file); BufferedWriter bufferedWriter = new BufferedWriter(fileWriter); bufferedWriter.write(content); bufferedWriter.close(); //read FileReader fileReader = new FileReader(file); BufferedReader bufferedReader =…

Read more »

Tạo chuỗi ký tự tùy biến theo độ dài chuỗi

By |

package quan.tutorial.com; import java.util.Random; public class GetRandomCharacters { public static String characters = “0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz”; public static void main(String[] args) { String randomCharacters = getRandomCharacters(10); System.out.println(“randomCharacters: ‘” + randomCharacters + “‘”); } public static String getRandomCharacters(int lenght) { String alphabet = new String(characters); int n = alphabet.length(); String result = new String(); Random r = new…

Read more »