public class Run{
public static void main(String[] args) {
try {
java.text.SimpleDateFormat formater = new java.text.SimpleDateFormat("HH mm ss");
java.util.Date n = new java.util.Date();
String start = "08 00 00";
String end = "18 00 00";
String now = formater.format(n);
java.util.Date time1 = formater.parse(start);
java.util.Date time2 = formater.parse(end);
java.util.Date time3 = formater.parse(now);
long l1 = time1.getTime();
long l2 = time2.getTime();
long l3 = time3.getTime();
double difference = (l3 - l1) / 1000;
difference = (difference < 0 ? -difference : difference) / 60;
difference = difference / 60;
int a = (int) difference;
double d = (l2 - l3) / 1000;
d = (d < 0 ? -d : d) / 60;
d = d / 60;
int b = (int) d;
System.out.println("a : " + a);
System.out.println("b : " + b);
if ((0 <= a && a < 10) && (0 <= b && b < 10)) {
System.out.println("OK");
}
} catch (java.text.ParseException e) {
e.printStackTrace();
}
}
}
Showing posts with label Java Example. Show all posts
Showing posts with label Java Example. Show all posts
Monday, May 4, 2015
Monday, March 3, 2014
Thursday, November 28, 2013
Azərbaycan Respublikasının Mərkəzi Bankı (Milli valyuta)
import java.io.InputStream;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.xml.parsers.*;
import org.w3c.dom.*;
public class Running {
public static void main(String[] args) {
try {
SimpleDateFormat ff = new SimpleDateFormat("dd.MM.yyyy");
URL url = new URL("http://cbar.az/currencies/" + ff.format(new Date()) + ".xml");
InputStream inputStream = url.openConnection().getInputStream();
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(inputStream);
doc.getDocumentElement().normalize();
NodeList nList = doc.getElementsByTagName("Valute");
for (int temp = 0; temp < nList.getLength(); temp++) {
Node nNode = nList.item(temp);
if (nNode.getNodeType() == Node.ELEMENT_NODE) {
Element eElement = (Element) nNode;
if (eElement.getAttribute("Code").equals("USD")) {
System.out.println(eElement.getElementsByTagName("Name").item(0).getTextContent() + " : " + eElement.getElementsByTagName("Value").item(0).getTextContent());
}
if (eElement.getAttribute("Code").equals("EUR")) {
System.out.println(eElement.getElementsByTagName("Name").item(0).getTextContent() + " : " + eElement.getElementsByTagName("Value").item(0).getTextContent());
}
if (eElement.getAttribute("Code").equals("RUB")) {
System.out.println(eElement.getElementsByTagName("Name").item(0).getTextContent() + " : " + eElement.getElementsByTagName("Value").item(0).getTextContent());
}
if (eElement.getAttribute("Code").equals("XAU")) {
System.out.println(eElement.getElementsByTagName("Name").item(0).getTextContent() + " : " + eElement.getElementsByTagName("Value").item(0).getTextContent());
}
if (eElement.getAttribute("Code").equals("TRY")) {
System.out.println(eElement.getElementsByTagName("Name").item(0).getTextContent() + " : " + eElement.getElementsByTagName("Value").item(0).getTextContent());
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.xml.parsers.*;
import org.w3c.dom.*;
public class Running {
public static void main(String[] args) {
try {
SimpleDateFormat ff = new SimpleDateFormat("dd.MM.yyyy");
URL url = new URL("http://cbar.az/currencies/" + ff.format(new Date()) + ".xml");
InputStream inputStream = url.openConnection().getInputStream();
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(inputStream);
doc.getDocumentElement().normalize();
NodeList nList = doc.getElementsByTagName("Valute");
for (int temp = 0; temp < nList.getLength(); temp++) {
Node nNode = nList.item(temp);
if (nNode.getNodeType() == Node.ELEMENT_NODE) {
Element eElement = (Element) nNode;
if (eElement.getAttribute("Code").equals("USD")) {
System.out.println(eElement.getElementsByTagName("Name").item(0).getTextContent() + " : " + eElement.getElementsByTagName("Value").item(0).getTextContent());
}
if (eElement.getAttribute("Code").equals("EUR")) {
System.out.println(eElement.getElementsByTagName("Name").item(0).getTextContent() + " : " + eElement.getElementsByTagName("Value").item(0).getTextContent());
}
if (eElement.getAttribute("Code").equals("RUB")) {
System.out.println(eElement.getElementsByTagName("Name").item(0).getTextContent() + " : " + eElement.getElementsByTagName("Value").item(0).getTextContent());
}
if (eElement.getAttribute("Code").equals("XAU")) {
System.out.println(eElement.getElementsByTagName("Name").item(0).getTextContent() + " : " + eElement.getElementsByTagName("Value").item(0).getTextContent());
}
if (eElement.getAttribute("Code").equals("TRY")) {
System.out.println(eElement.getElementsByTagName("Name").item(0).getTextContent() + " : " + eElement.getElementsByTagName("Value").item(0).getTextContent());
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
Wednesday, August 22, 2012
JSoup Example
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.Jsoup;
import org.jsoup.select.Elements;
import java.net.URL;
import java.io.IOException;
public class JSoupExample {
public static void main(String[] args) throws IOException {
URL url = new URL("http://selcukoglu.blogspot.com/");
System.out.println("URL : " + url.toExternalForm());
Document doc = Jsoup.parse(url, 3 * 1000);
Elements types = doc.select("link[type]");
System.out.println("Size : (" + types.size() + ")");
for (Element link : types) {
System.out.println("*");
System.out.println(" link[type] : " + link.tagName());
System.out.println(" href : " + link.attr("abs:href"));
System.out.println(" rel : " + link.attr("rel"));
}
}
}
import org.jsoup.nodes.Element;
import org.jsoup.Jsoup;
import org.jsoup.select.Elements;
import java.net.URL;
import java.io.IOException;
public class JSoupExample {
public static void main(String[] args) throws IOException {
URL url = new URL("http://selcukoglu.blogspot.com/");
System.out.println("URL : " + url.toExternalForm());
Document doc = Jsoup.parse(url, 3 * 1000);
Elements types = doc.select("link[type]");
System.out.println("Size : (" + types.size() + ")");
for (Element link : types) {
System.out.println("*");
System.out.println(" link[type] : " + link.tagName());
System.out.println(" href : " + link.attr("abs:href"));
System.out.println(" rel : " + link.attr("rel"));
}
}
}
Tuesday, July 3, 2012
Date increment hour
public static void main(String[] args) {
try {
Calendar calendar = Calendar.getInstance();
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
calendar.setTime(format.parse("2012-06-30 18:30:12"));
System.out.println("Original = " + format.format(calendar.getTime()));
calendar.add(Calendar.HOUR, 5);
calendar.add(Calendar.MINUTE, 00);
calendar.add(Calendar.SECOND, 0);
System.out.println("Updated = " + format.format(calendar.getTime()));
} catch (Exception e) {
}
}
Calendar calendar = Calendar.getInstance();
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
calendar.setTime(format.parse("2012-06-30 18:30:12"));
System.out.println("Original = " + format.format(calendar.getTime()));
calendar.add(Calendar.HOUR, 5);
calendar.add(Calendar.MINUTE, 00);
calendar.add(Calendar.SECOND, 0);
System.out.println("Updated = " + format.format(calendar.getTime()));
} catch (Exception e) {
}
Friday, June 22, 2012
Date increment days
public static void main(String[] args) {
try {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
Date startDate = (Date) format.parse("2012-06-22");;
String start = format.format(startDate);
String end = format.format(addDays(startDate, 9));
System.out.println("Start : " + start);
System.out.println("End : " + end);
} catch (Exception e) {
e.printStackTrace();
}
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
Date startDate = (Date) format.parse("2012-06-22");;
String start = format.format(startDate);
String end = format.format(addDays(startDate, 9));
System.out.println("Start : " + start);
System.out.println("End : " + end);
} catch (Exception e) {
e.printStackTrace();
}
}
public static Date addDays(Date date, int days)
{
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.add(Calendar.DATE, days);
return cal.getTime();
}
Thursday, June 7, 2012
Iki Data Arasinda Aylarin Hesablanmasi
public static int months(Date start, Date end) {
int count = 0;
Calendar cal = Calendar.getInstance();
cal.setTime(start);
int startDate = cal.get(Calendar.DATE);
int startMonth = cal.get(Calendar.MONTH);
int startYear = cal.get(Calendar.YEAR);
cal.setTime(end);
int endDate = cal.get(Calendar.DATE);
int endMonth = cal.get(Calendar.MONTH);
int endYear = cal.get(Calendar.YEAR);
count = ((endYear - startYear) * 12) + (endMonth - startMonth) + ((endDate >= startDate) ? 1 : 0);
return count - 1;
}
int count = 0;
Calendar cal = Calendar.getInstance();
cal.setTime(start);
int startDate = cal.get(Calendar.DATE);
int startMonth = cal.get(Calendar.MONTH);
int startYear = cal.get(Calendar.YEAR);
cal.setTime(end);
int endDate = cal.get(Calendar.DATE);
int endMonth = cal.get(Calendar.MONTH);
int endYear = cal.get(Calendar.YEAR);
count = ((endYear - startYear) * 12) + (endMonth - startMonth) + ((endDate >= startDate) ? 1 : 0);
return count - 1;
}
Thursday, May 24, 2012
Java'da Windows işletim sisteminin ekranını kilitleme
public class Runnig {
public static void main(String[] args) {
try {
Runtime.getRuntime().exec("C:\\\\Windows\\\\System32\\\\rundll32.exe user32.dll,LockWorkStation");
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
try {
Runtime.getRuntime().exec("C:\\\\Windows\\\\System32\\\\rundll32.exe user32.dll,LockWorkStation");
} catch (IOException e) {
e.printStackTrace();
}
}
}
Thursday, April 12, 2012
Date between days list
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
public class TestDate {
public static void main(String[] args) {
try {
List days = new ArrayList();
String start = "21/07/2011";
String end = "08/08/2011";
DateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
DateFormat outFormatter = new SimpleDateFormat("yyyy-MM-dd");
Date startD = (Date) formatter.parse(start);
Date endD = (Date) formatter.parse(end);
long ara = 24 * 1000 * 60 * 60;
long endTime = endD.getTime();
long time = startD.getTime();
while (time <= endTime) {
days.add(new Date(time));
time += ara;
}
for (int i = 0; i < days.size(); i++) {
Date d = (Date) days.get(i);
String day = outFormatter.format(d);
System.out.println(day);
}
} catch (ParseException ex) {
ex.printStackTrace();
}
}
}
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
public class TestDate {
public static void main(String[] args) {
try {
List
String start = "21/07/2011";
String end = "08/08/2011";
DateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
DateFormat outFormatter = new SimpleDateFormat("yyyy-MM-dd");
Date startD = (Date) formatter.parse(start);
Date endD = (Date) formatter.parse(end);
long ara = 24 * 1000 * 60 * 60;
long endTime = endD.getTime();
long time = startD.getTime();
while (time <= endTime) {
days.add(new Date(time));
time += ara;
}
for (int i = 0; i < days.size(); i++) {
Date d = (Date) days.get(i);
String day = outFormatter.format(d);
System.out.println(day);
}
} catch (ParseException ex) {
ex.printStackTrace();
}
}
}
Tuesday, April 10, 2012
Clipboard
class ClipboardListener extends Thread implements ClipboardOwner {
Clipboard sysClip = Toolkit.getDefaultToolkit().getSystemClipboard();
boolean bEnough = false;
@Override
public void run() {
Transferable trans = sysClip.getContents(this);
regainOwnership(trans);
while (true) {
if (isitEnough()) {
break;
}
}
}
public void itisEnough() {
bEnough = true;
}
public void itisNotEnough() {
bEnough = false;
}
boolean isitEnough() {
return bEnough;
}
@Override
public void lostOwnership(Clipboard c, Transferable t) {
try {
sleep(200);
} catch (Exception e) {
System.out.println("Exception: " + e);
}
try {
Transferable contents = c.getContents(this);
regainOwnership(contents);
} catch (Exception e) {
e.printStackTrace();
}
}
void processContents(Transferable t) {
if (isitEnough()) {
return;
}
DataFlavor[] flavors = t.getTransferDataFlavors();
for (int i = flavors.length - 1; i >= 0; i--) {
try {
Object o = t.getTransferData(flavors[i]);
if (o instanceof String) {
System.out.println(o.toString().toLowerCase());
break;
}
} catch (UnsupportedFlavorException | IOException exp) {
exp.printStackTrace();
}
}
}
void regainOwnership(Transferable t) {
sysClip.setContents(t, this);
processContents(t);
}
public static void main(String[] args) {
ClipboardListener b = new ClipboardListener();
b.itisNotEnough();
b.start();
}
}
Clipboard sysClip = Toolkit.getDefaultToolkit().getSystemClipboard();
boolean bEnough = false;
@Override
public void run() {
Transferable trans = sysClip.getContents(this);
regainOwnership(trans);
while (true) {
if (isitEnough()) {
break;
}
}
}
public void itisEnough() {
bEnough = true;
}
public void itisNotEnough() {
bEnough = false;
}
boolean isitEnough() {
return bEnough;
}
@Override
public void lostOwnership(Clipboard c, Transferable t) {
try {
sleep(200);
} catch (Exception e) {
System.out.println("Exception: " + e);
}
try {
Transferable contents = c.getContents(this);
regainOwnership(contents);
} catch (Exception e) {
e.printStackTrace();
}
}
void processContents(Transferable t) {
if (isitEnough()) {
return;
}
DataFlavor[] flavors = t.getTransferDataFlavors();
for (int i = flavors.length - 1; i >= 0; i--) {
try {
Object o = t.getTransferData(flavors[i]);
if (o instanceof String) {
System.out.println(o.toString().toLowerCase());
break;
}
} catch (UnsupportedFlavorException | IOException exp) {
exp.printStackTrace();
}
}
}
void regainOwnership(Transferable t) {
sysClip.setContents(t, this);
processContents(t);
}
public static void main(String[] args) {
ClipboardListener b = new ClipboardListener();
b.itisNotEnough();
b.start();
}
}
Double Format
public class Main {
public static void main(String[] args) {
java.text.DecimalFormat df = new java.text.DecimalFormat("###.##");
double val = 63.5666;
try {
val = df.parse(df.format(val)).doubleValue();
System.out.println(val);
} catch (ParseException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
public static void main(String[] args) {
java.text.DecimalFormat df = new java.text.DecimalFormat("###.##");
double val = 63.5666;
try {
val = df.parse(df.format(val)).doubleValue();
System.out.println(val);
} catch (ParseException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
Saturday, April 7, 2012
MS Access Connection
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import sun.jdbc.odbc.JdbcOdbcDriver;
public class Main {
public static void main(String[] args) {
try {
DriverManager.registerDriver(new JdbcOdbcDriver());
Connection connection = DriverManager.getConnection("jdbc:odbc:selcukogluODBC");
Statement statement = connection.createStatement();
ResultSet rs = statement.executeQuery("Select * From Student");
while(rs.next()){
System.out.println(rs.getInt(1) +" "+rs.getString(2)+" "+rs.getString(3)+" "+rs.getInt(4));
}
} catch (SQLException ex) {
ex.printStackTrace();
}
}
}
Monday, March 5, 2012
Append Output to File Using FileOutputStream
Bu yazimda FileOutputStream sinifi ile yazdigim bir text dosyasinin uzerine tekrar baska yazilarida eklemek. Eyer siz bu sinif yardimi ile text yazi yazarsaniz her zaman text en son yazdiginizi gorursunuz. Ama FileOutputStream sinifini CONSTURUKTOR-nun ikinci parametresini FileOutputStream(path,true) bu seklide yazarsaniz text-teki yazilar kalir ve yeni yazida elave edilir.
public static void main(String[] args) {
String path = "C://Demo.txt";
try {
FileOutputStream fos = new FileOutputStream(path,true);
String name = "Abdulkadir Selcukoglu ";
fos.write(name.getBytes());
fos.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}
public static void main(String[] args) {
String path = "C://Demo.txt";
try {
FileOutputStream fos = new FileOutputStream(path,true);
String name = "Abdulkadir Selcukoglu ";
fos.write(name.getBytes());
fos.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}
Thursday, March 1, 2012
GMail Send
package az.mb;
import java.util.Properties;
import javax.annotation.Resource;
import javax.mail.Message;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class GMail {
@Resource(name = "vpdmail")
private Session mailSession;
public void sendMessage(String email,String head,String mesaj) throws Exception {
java.security.Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
boolean debug = true;
String SMTP_HOST_NAME = "smtp.gmail.com";
String SMTP_PORT = "465";
String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
Properties props = new Properties();
props.put("mail.smtp.host", SMTP_HOST_NAME);
props.put("mail.smtp.auth", "true");
props.put("mail.debug", "true");
props.put("mail.smtp.port", SMTP_PORT);
props.put("mail.smtp.socketFactory.port", SMTP_PORT);
props.put("mail.smtp.socketFactory.class", SSL_FACTORY);
props.put ("mail.smtp.socketFactory.fallback", "false");
mailSession = Session.getInstance (props,
new javax.mail.Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("email", "password");
}
});
mailSession.setDebug(debug);
Message msg = new MimeMessage(mailSession);
msg.setSubject(head);
msg.setRecipients (Message.RecipientType.TO,
InternetAddress.parse(email));
msg.setText(mesaj);
Transport.send(msg);
System.out.println("message sent");
}
}
import java.util.Properties;
import javax.annotation.Resource;
import javax.mail.Message;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class GMail {
@Resource(name = "vpdmail")
private Session mailSession;
public void sendMessage(String email,String head,String mesaj) throws Exception {
java.security.Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
boolean debug = true;
String SMTP_HOST_NAME = "smtp.gmail.com";
String SMTP_PORT = "465";
String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
Properties props = new Properties();
props.put("mail.smtp.host", SMTP_HOST_NAME);
props.put("mail.smtp.auth", "true");
props.put("mail.debug", "true");
props.put("mail.smtp.port", SMTP_PORT);
props.put("mail.smtp.socketFactory.port", SMTP_PORT);
props.put("mail.smtp.socketFactory.class", SSL_FACTORY);
props.put ("mail.smtp.socketFactory.fallback", "false");
mailSession = Session.getInstance (props,
new javax.mail.Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("email", "password");
}
});
mailSession.setDebug(debug);
Message msg = new MimeMessage(mailSession);
msg.setSubject(head);
msg.setRecipients (Message.RecipientType.TO,
InternetAddress.parse(email));
msg.setText(mesaj);
Transport.send(msg);
System.out.println("message sent");
}
}
Thursday, January 26, 2012
Jar Windows Service Make
C:\Users\abdulkadirs\Documents\NetBeansProjects\MikserSMSandEMAIL_26122011\Mikse
rSMSandEMAIL\dist>sc create "BenimServicem" binPath= "C:\Program Files\Java\jdk
1.6.0_13\bin\java.exe -jar MikserSMSandEMAIL.jar " type= own start= auto error=
ignore
rSMSandEMAIL\dist>sc create "BenimServicem" binPath= "C:\Program Files\Java\jdk
1.6.0_13\bin\java.exe -jar MikserSMSandEMAIL.jar " type= own start= auto error=
ignore
Tuesday, December 13, 2011
java Date between timer
public class Main {
public static void main(String args[]) {
boolean isOK =true;
int time = 0 ;
Calendar cal1 = new GregorianCalendar();
while(isOK){
Calendar cal2 = new GregorianCalendar();
time = (int) ((cal2.getTime().getTime() - cal1.getTime().getTime()) / 1000);
System.out.println(time);
if(time == 5){
isOK = false;
}
}
System.out.println("Days= " + time);
}
}
public static void main(String args[]) {
boolean isOK =true;
int time = 0 ;
Calendar cal1 = new GregorianCalendar();
while(isOK){
Calendar cal2 = new GregorianCalendar();
time = (int) ((cal2.getTime().getTime() - cal1.getTime().getTime()) / 1000);
System.out.println(time);
if(time == 5){
isOK = false;
}
}
System.out.println("Days= " + time);
}
}
Saturday, October 1, 2011
Kim Tez Oturdu
public class Runing {
public static void main(String[] args) {
Scanner h=new Scanner(System.in);
int a= h.nextInt();
int massiv[]=new int [a];
int ixt= (int) ( (a-1)*Math.random()+1);
for(int i=0;i massiv[i]=i+1;
System.out.println("massivin olcusu="+a);
System.out.println("novbe="+ixt);
//Esas program
int d=0 ;int c=-1;
for (int i=0;i
d=0;
while (d
c++;
if (massiv[c%a]!=0) d++;
}
massiv[c%a]=0;
}
for (int i=0;i if (massiv[i]!=0) System.out.println("cavab="+(i+1));
}
}
public static void main(String[] args) {
Scanner h=new Scanner(System.in);
int a= h.nextInt();
int massiv[]=new int [a];
int ixt= (int) ( (a-1)*Math.random()+1);
for(int i=0;i massiv[i]=i+1;
System.out.println("massivin olcusu="+a);
System.out.println("novbe="+ixt);
//Esas program
int d=0 ;int c=-1;
for (int i=0;i
while (d
if (massiv[c%a]!=0) d++;
}
massiv[c%a]=0;
}
for (int i=0;i if (massiv[i]!=0) System.out.println("cavab="+(i+1));
}
}
Tuesday, May 17, 2011
Java SE Uygulamalari
Asagidaki Sayfadan Kopyalanmis.
http://www.ethemsulan.com/category/javada-duzenli-ifadelerregular-expression-java-examples
Validate IPV4 Address Using Java Regular Expression(Düzenli Ifade ile IPV4 Geçerliliğini Denetleyen Java Örneği)
Mart 7, 2010 | Comments | Java'da Düzenli Ifadeler(Regular Expression Java Examples)Orj kod:http://www.ethemsulan.pastebin.com/yw9vp2iNpackage www.ethemsulan.com;import java.util.regex.PatternSyntaxException;import javax.swing.JOptionPane;public class IpV4Kontrolu {public static void main(String[] args) {try {String girilenIp=JOptionPane.showInputDialog("Bir IpV4 adresi girin");if (girilenIp.matches("\\b(25[0-5]|2[0-4][0-9]" +"|[01]?[0-9][0-9]?)\\.(25[0-5]|2[0-4][0-9]|[01]?" +"[0-9][0-9]?)\\.(25[0-5]|2[0-4][0-9]|[01]?[0-9]" +"[0-9]?)\\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\b")){JOptionPane.showMessageDialog(null, girilenIp+" dogru IpV4 adresi girildi");} else {JOptionPane.showMessageDialog(null, girilenIp+" yanlış IpV4 adresi girildi");}} catch (PatternSyntaxException ex) {ex.printStackTrace();//127.0.0.1 dogru ipv4 localhost adresi//299.299.299.299 yanlış 255 ten büyük olamaz.değerler her bir aralık//255.255.255.256 yanlış max 255 olur.//0:0:0:0:0:0:0:1 ipv6//92.168.1.1 doğr ipv4 adresi} } }
Javada Düzenli Ifade ile Sayı Kontrolu(Check String and Digits Regular Expression Java Example)
Şubat 21, 2010 | Comments | Java'da Düzenli Ifadeler(Regular Expression Java Examples)Bu örnek ile aynı işlevi gerçekleştiren başka bir kod :http://www.ethemsulan.com/2010/01/girilen-degerin-say-ya-da-string.html bakın.Yanı düzenli ifadeler bir çok farklı şekilde yazılabilir.package www.ethemsulan.com;import javax.swing.JOptionPane;public class StringInt {public static void main(String[] args) {String kontrol=JOptionPane.showInputDialog("Bir sayi ya da string gir");//girilen deger sayi yada string oldugunu bulurif(kontrol.matches("^\\d+$")){System.out.println("Girilen deger sayidir-->"+kontrol);}else{System.out.println("Girilen deger stringtir-->"+kontrol);} } }
Java’da Rakamları Bulan Düzenli Ifade Örneği
Şubat 20, 2010 | Comments | Java'da Düzenli Ifadeler(Regular Expression Java Examples)Eğer [0-9]+ yazarsak o zaman 21 ve 2010 u ayrı ayrı bulur.Ama bu şekilde 2,1,2,0,1,0 şeklinde yazar.+ bir ve birden fazla demek.package www.ethemsulan.com;import java.util.regex.Pattern;import java.util.regex.Matcher;public class RakamLari {public static void main(String[] args){//rakamalri bulur.Bun tur orneklerin daha kisa yolu olabilirPattern kal2=Pattern.compile("[0-9]");Matcher esl2=kal2.matcher("21 Subat 2010 tarihinde yazildi");while(esl2.find()){System.out.println("Rakamlar-->"+esl2.group());} } }
Java ile Belirten Harfler ile Başlayıp Biten Kelimeyi Bulan Düzenli Ifade Kodu(compile(),find(),matcher(),group())
Şubat 20, 2010 | Comments | Java'da Düzenli Ifadeler(Regular Expression Java Examples)orj kod:http://www.ethemsulan.pastebin.com/f61c85875package www.ethemsulan.com;import java.util.regex.Matcher;import java.util.regex.Pattern;public class RegexKullanimi {public static void main(String[] args) {//m ile biten ve E ile baslayan kelimeleri bulur.Pattern kalip=Pattern.compile("E.+?m");Matcher esl=kalip.matcher("Bu cumlede Ethem tek " +"eslesiyor.Evkarlandim bu gun salla");while (esl.find()) {System.out.println("Eslesen-->"+esl.group());} } }
Javada Tam Sayı Kontrolü Yapan Düzenli Ifade Kodu(How to Integer Control In Java with Regular Expression)
Şubat 13, 2010 | Comments | Java'da Düzenli Ifadeler(Regular Expression Java Examples)Orj kod:http://www.ethemsulan.pastebin.com/f222c383dpackage www.ethemsulan.com;import java.util.regex.PatternSyntaxException;import javax.swing.JOptionPane;public class SayiKontrol {public static void main(String[] args) {try {String girilenDeger=JOptionPane.showInputDialog("Tam sayi ya da string gir");if (girilenDeger.matches("(?simx)(?")) {int tamsayi=Integer.parseInt(girilenDeger);System.out.println("Tam sayı girildi: "+tamsayi);} else {System.out.println("dize ya da char girildi:"+girilenDeger);}} catch (PatternSyntaxException ex) {ex.printStackTrace();} } }
Eğer (?simx)(?
Email Kontrolü yapan Java Örneği(How to Find or Validate an Email Address Java Example)
Ocak 7, 2010 | Comments | Java'da Düzenli Ifadeler(Regular Expression Java Examples)Orj Kod:http://ethemsulan.pastebin.com/f11eb0376package www.ethemsulan.com;import javax.swing.JOptionPane;public class EmailKontrol {public static void main(String[] args) {String email=JOptionPane.showInputDialog("E-mail adresinizi girin");//(?simx)\\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\\.[A-Z]{2,4}\\b//girilend egerin email olup olmadigini kontrol ediyorif (email.matches("(?simx)\\b[A-Z0-9._%+-]+" +"@[A-Z0-9.-]+\\.[A-Z]{2,4}\\b")) {System.out.println(email+" email adresidir");} else {System.out.println(email+" email degil");}}}
Girilen değerin sayı ya da string olduğunu bulan java örneği(Regex String or Integer)
Ocak 6, 2010 | Comments | Java'da Düzenli Ifadeler(Regular Expression Java Examples)Orj Kod:http://ethemsulan.pastebin.com/f6b3d144fpackage www.ethemsulan.com;import java.util.regex.PatternSyntaxException;import javax.swing.JOptionPane;public class GirilenDegerSayimi {public static void main(String[] args) {String sayimi=JOptionPane.showInputDialog("Rakam ya da harf girin");try {if (sayimi.matches("(?")) {System.out.println("Girilen "+sayimi+" bir rakam ya da sayidir");} else {System.out.println(sayimi+" sayi degil bir kelime ya da harftir");}} catch (PatternSyntaxException ex) {ex.printStackTrace();}}}
Girilen değerin sayı ya da string olduğunu bulur.Düzenli ifadeler çok kullanışlıdır.Kod yazmaktan kurtarır.
yy-mm-dd düzenli ifadenin(Regular Expression) java örneği
Ocak 4, 2010 | Comments | Java'da Düzenli Ifadeler(Regular Expression Java Examples)Orj Kod:http://ethemsulan.pastebin.com/f3f154fd9package www.ethemsulan.com;import javax.swing.JOptionPane;public class TarihKontrol {public static void main(String[] args) {//tarih formatini kontrol eden duzenli ifadedirString dateTarih=JOptionPane.showInputDialog("Tarih girin format yyy-aa-gg seklinde");if (dateTarih.matches("\\d{4}-(0[1-9]|1[012])-(0[1-9]|[12][0-9]|3[01])")) {System.out.println("dogru");} else {System.out.println("yanlis");}}}
4 rakamdan oluşan yil, iki rakamdan oluşan ay ve son ikisi de gün ve tanımlı oldukları aralığı kontrol eder.örneğin 2010-04-01 doğru. 2010-89-10 yanliş.
hh:mm:ss düzenli ifadenin java örneği(hh:mm:ss regex)
Ocak 4, 2010 | Comments | Java'da Düzenli Ifadeler(Regular Expression Java Examples)package www.ethemsulan.com;import javax.swing.JOptionPane;public class TimeSaatKontrol {public static void main(String[] args) {//saat ss:dd:ss veya ss:dd kontrol eder.String dateTarih=JOptionPane.showInputDialog("Saat girin format ss:dd:ss ya da ss:dd seklinde");if (dateTarih.matches("^(([0-1]?[0-9])|([2][0-3])):([0-5]?[0-9])" +"(:([0-5]?[0-9]))?$")){System.out.println("dgoru");}else{System.out.println("yanlis");}}}
Wednesday, November 10, 2010
JXPATH Uygulamalari
1.Uygulama :
public class Employee {
private String name;
private String surname;
private int age;
public Employee() {
}
public Employee(String name, String surname, int age) {
this.name = name;
this.surname = surname;
this.age = age;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSurname() {
return surname;
}
public void setSurname(String surname) {
this.surname = surname;
}
}
private String name;
private String surname;
private int age;
public Employee() {
}
public Employee(String name, String surname, int age) {
this.name = name;
this.surname = surname;
this.age = age;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSurname() {
return surname;
}
public void setSurname(String surname) {
this.surname = surname;
}
}
public class Main {
public static void main(String[] args) {
Employee e = new Employee();
e.setName("Abdulkadir");
e.setSurname("Selcukoglu");
e.setAge(27);
JXPathContext context = JXPathContext.newContext(e);
Object n = context.getValue("name");
System.out.println(n);
}
}public static void main(String[] args) {
Employee e = new Employee();
e.setName("Abdulkadir");
e.setSurname("Selcukoglu");
e.setAge(27);
JXPathContext context = JXPathContext.newContext(e);
Object n = context.getValue("name");
System.out.println(n);
}
//Console Output
Abdulkadir2.Uygulama :
public class Employee {
private String name;
private String surname;
private int age;
public Employee() {
}
public Employee(String name, String surname, int age) {
this.name = name;
this.surname = surname;
this.age = age;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSurname() {
return surname;
}
public void setSurname(String surname) {
this.surname = surname;
}
}
private String name;
private String surname;
private int age;
public Employee() {
}
public Employee(String name, String surname, int age) {
this.name = name;
this.surname = surname;
this.age = age;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSurname() {
return surname;
}
public void setSurname(String surname) {
this.surname = surname;
}
}
public class EmployeeBean {
List list = new ArrayList();
public EmployeeBean() {
list.add(new Employee("Abdulkadir","Selcukoglu", 27));
list.add(new Employee("Ertugrul","Aslan", 26));
list.add(new Employee("Rasim","Imanov", 35));
list.add(new Employee("Nazim","Memedov", 25));
list.add(new Employee("Murat","Tagiyev", 24));
}
public List getList() {
return list;
}
public void setList(List list) {
this.list = list;
}
}
List
public EmployeeBean() {
list.add(new Employee("Abdulkadir","Selcukoglu", 27));
list.add(new Employee("Ertugrul","Aslan", 26));
list.add(new Employee("Rasim","Imanov", 35));
list.add(new Employee("Nazim","Memedov", 25));
list.add(new Employee("Murat","Tagiyev", 24));
}
public List
return list;
}
public void setList(List
this.list = list;
}
}
public class Main {
public static void main(String[] args) {
EmployeeBean b = new EmployeeBean();
for (Employee e : b.list) {
JXPathContext context = JXPathContext.newContext(e);
Object n = context.getValue("name");
Object s = context.getValue("surname");
Object a = context.getValue("age");
System.out.println(n + " "+s+" "+a);
}
}
EmployeeBean b = new EmployeeBean();
for (Employee e : b.list) {
JXPathContext context = JXPathContext.newContext(e);
Object n = context.getValue("name");
Object s = context.getValue("surname");
Object a = context.getValue("age");
System.out.println(n + " "+s+" "+a);
}
}
}
//Console Output
Abdulkadir Selcukoglu 27
Ertugrul Aslan 26
Rasim Imanov 35
Nazim Memedov 25
Murat Tagiyev 24
Ertugrul Aslan 26
Rasim Imanov 35
Nazim Memedov 25
Murat Tagiyev 24
2.Uygulama :
public class Address {
private String country;
private String city;
public Address() {
}
public Address(String country, String city) {
this.country = country;
this.city = city;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
}
private String country;
private String city;
public Address() {
}
public Address(String country, String city) {
this.country = country;
this.city = city;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
}
public class Employee {
private String name;
private String surname;
private int age;
private Address address;
public Employee() {
}
public Employee(String name, String surname, int age, Address address) {
this.name = name;
this.surname = surname;
this.age = age;
this.address = address;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSurname() {
return surname;
}
public void setSurname(String surname) {
this.surname = surname;
}
}
private String name;
private String surname;
private int age;
private Address address;
public Employee() {
}
public Employee(String name, String surname, int age, Address address) {
this.name = name;
this.surname = surname;
this.age = age;
this.address = address;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSurname() {
return surname;
}
public void setSurname(String surname) {
this.surname = surname;
}
}
public class EmployeeBean {
List list = new ArrayList();
public EmployeeBean() {
list.add(new Employee("Abdulkadir","Selcukoglu", 27,new Address("Turkiye","Mardin")));
list.add(new Employee("Ertugrul","Aslan", 26,new Address("Turkiye","Gaziantep")));
list.add(new Employee("Rasim","Imanov", 33,new Address("Gurcistan","Tiflis")));
list.add(new Employee("Nazim","Memedov", 25,new Address("Gurcistan","Tiflis")));
list.add(new Employee("Murat","Tagiyev", 26,new Address("Azerbaycan","Baku")));
}
public List getList() {
return list;
}
public void setList(List list) {
this.list = list;
}
}
List
public EmployeeBean() {
list.add(new Employee("Abdulkadir","Selcukoglu", 27,new Address("Turkiye","Mardin")));
list.add(new Employee("Ertugrul","Aslan", 26,new Address("Turkiye","Gaziantep")));
list.add(new Employee("Rasim","Imanov", 33,new Address("Gurcistan","Tiflis")));
list.add(new Employee("Nazim","Memedov", 25,new Address("Gurcistan","Tiflis")));
list.add(new Employee("Murat","Tagiyev", 26,new Address("Azerbaycan","Baku")));
}
public List
return list;
}
public void setList(List
this.list = list;
}
}
public class Main {
public static void main(String[] args) {
Address a = new Address();
a.setCountry("Turkiye");
a.setCity("Mardin");
Employee e = new Employee();
e.setName("Abdulkadir");
e.setSurname("Selcukoglu");
e.setAge(27);
e.setAddress(a);
JXPathContext context = JXPathContext.newContext(e);
Object ulke = context.getValue("address/country");
System.out.println(ulke);
}
}
//Console Output
Turkiye
3.Uygulama :
public class Names {
private String nameList[];
public Names() {
}
public Names(String[] nameList) {
this.nameList = nameList;
}
public String[] getNameList() {
return nameList;
}
public void setNameList(String[] nameList) {
this.nameList = nameList;
}
}
private String nameList[];
public Names() {
}
public Names(String[] nameList) {
this.nameList = nameList;
}
public String[] getNameList() {
return nameList;
}
public void setNameList(String[] nameList) {
this.nameList = nameList;
}
}
public class Main {
public static void main(String args[]){
String list[]={"Abdulkadir","Ertugrul","Rasim","Nazim"};
Names n=new Names();
n.setNameList(list);
JXPathContext context = JXPathContext.newContext(n);
Object o[]= (Object[]) context.getValue("nameList");
System.out.println(o[0]);
System.out.println(o[1]);
System.out.println(o[2]);
System.out.println(o[3]);
}
}
public static void main(String args[]){
String list[]={"Abdulkadir","Ertugrul","Rasim","Nazim"};
Names n=new Names();
n.setNameList(list);
JXPathContext context = JXPathContext.newContext(n);
Object o[]= (Object[]) context.getValue("nameList");
System.out.println(o[0]);
System.out.println(o[1]);
System.out.println(o[2]);
System.out.println(o[3]);
}
}
//Console Output
Abdulkadir
Ertugrul
Rasim
Nazim
4.Uygulama :
public class Author {
Book books[];
public Book[] getBooks() {
return books;
}
public void setBooks(Book[] books) {
this.books = books;
}
}
public class Main {
public static void main(String args[]) {
Author author = new Author();
Book bs[] = new Book[3];
bs[0] = new Book();
bs[0].setName("Hz Muhammed'in Hayati");
bs[1] = new Book();
bs[1].setName("Sahabalerin Hayati");
bs[2] = new Book();
bs[2].setName("Guzel Ahlak");
author.setBooks(bs);
JXPathContext context = JXPathContext.newContext(author);
Book b[]= (Book[]) context.getValue("books");
for (Book k : b) {
System.out.println(k.getName());
}
}
}
Sahabalerin Hayati
Guzel Ahlak
5.Uygulama :
public class Author {
Book books[];
public Book[] getBooks() {
return books;
}
public void setBooks(Book[] books) {
this.books = books;
}
}
Ertugrul
Rasim
Nazim
4.Uygulama :
public class Book {
private String name;
public Book() {
}
public Book(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
private String name;
public Book() {
}
public Book(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
public class Author {
Book books[];
public Book[] getBooks() {
return books;
}
public void setBooks(Book[] books) {
this.books = books;
}
}
public class Main {
public static void main(String args[]) {
Author author = new Author();
Book bs[] = new Book[3];
bs[0] = new Book();
bs[0].setName("Hz Muhammed'in Hayati");
bs[1] = new Book();
bs[1].setName("Sahabalerin Hayati");
bs[2] = new Book();
bs[2].setName("Guzel Ahlak");
author.setBooks(bs);
JXPathContext context = JXPathContext.newContext(author);
Book b[]= (Book[]) context.getValue("books");
for (Book k : b) {
System.out.println(k.getName());
}
}
}
//Console Output
Hz Muhammed'in HayatiSahabalerin Hayati
Guzel Ahlak
5.Uygulama :
public class Book {
private String name;
public Book() {
}
public Book(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
private String name;
public Book() {
}
public Book(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
public class Author {
Book books[];
public Book[] getBooks() {
return books;
}
public void setBooks(Book[] books) {
this.books = books;
}
}
public class Main {
public static void main(String args[]) {
Author author = new Author();
Book bs[] = new Book[3];
bs[0] = new Book();
bs[0].setName("Hz Muhammed'in Hayati");
bs[1] = new Book();
bs[1].setName("Sahabalerin Hayati");
bs[2] = new Book();
bs[2].setName("Guzel Ahlak");
author.setBooks(bs);
JXPathContext context = JXPathContext.newContext(author);
Iterator iter = context.iterate("books[position() < 4]");
while(iter.hasNext()){
Book bb = (Book) iter.next();
System.out.println(bb.getName());
}
public static void main(String args[]) {
Author author = new Author();
Book bs[] = new Book[3];
bs[0] = new Book();
bs[0].setName("Hz Muhammed'in Hayati");
bs[1] = new Book();
bs[1].setName("Sahabalerin Hayati");
bs[2] = new Book();
bs[2].setName("Guzel Ahlak");
author.setBooks(bs);
JXPathContext context = JXPathContext.newContext(author);
Iterator iter = context.iterate("books[position() < 4]");
while(iter.hasNext()){
Book bb = (Book) iter.next();
System.out.println(bb.getName());
}
}
}//Console Output
Hz Muhammed'in HayatiSahabalerin Hayati
Guzel Ahlak
6.Uygulama
public class Address {
private String country;
private String city;
public Address() {
}
public Address(String country, String city) {
this.country = country;
this.city = city;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
}
public class Employee {
private Map addresses = new HashMap();
public Employee() {
addresses.put("home", new Address("Turkiye","Mardin"));
addresses.put("office", new Address("Azerbaycan","Baku"));
}
public Map getAddresses() {
return addresses;
}
public void setAddresses(Map addresses) {
this.addresses = addresses;
}
}
public class Main {
public static void main(String args[]) {
Employee emp = new Employee();
JXPathContext context = JXPathContext.newContext(emp);
String cou = (String) context.getValue("addresses/home/country");
String ci = (String) context.getValue("addresses/home/city");
System.out.println(cou);
System.out.println(ci);
String cou2 = (String) context.getValue("addresses/office/country");
String ci2 = (String) context.getValue("addresses/office/city");
System.out.println(cou2);
System.out.println(ci2);
}
}
//Console Output
Turkiye
Mardin
Azerbaycan
Baku
Etiketler:
Java Example,
Java Information
Subscribe to:
Posts (Atom)







