-- Print the total time taken to perform a task
package com.java.knots;
public class TotalTime {
public static void main(String[] args) {
long programStartTime = 0;
programStartTime = System.currentTimeMillis();
/*
* perform required task
*/
long programEndTime = System.currentTimeMillis();
long minutes = ((programEndTime - programStartTime)/1000) / 60;
long seconds = ((programEndTime - programStartTime)/1000) % 60;
System.out.println("Task performed in "+minutes+" Minutes "+seconds+" Seconds");
}
}
Try for below task!
for(int i=0; i<100000000; i++)
System.out.print("");
The output in my system was "Task performed in 0 Minutes 8 Seconds"
-- How to send an Email using Java code!
import java.io.UnsupportedEncodingException;
import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class SendEmail {
public static void main(String[] args) throws InterruptedException {
final String username = "deepak*****@gmail.com"; //supply your own userid
final String password = "********"; //supply your own password
String fromAddress = "deepak*****@gmail.com";
String toAddresses = "deepak******@gmail.com"; //,rohitvishwakarma06@gmail.com";
Properties props = new Properties();
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.port", "587");
Session session = Session.getInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
});
try {
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(fromAddress));
message.setRecipients(Message.RecipientType.TO,InternetAddress.parse(toAddresses));
message.setSubject("Test#1");
message.setText("I am glad that it is working...!");
Transport.send(message);
System.out.println("Done");
} catch (MessagingException e) {
throw new RuntimeException(e);
}
}
}
-- How to use TimerTask for cronjobs in Java
Main.java
package com.java.knots;
public class TotalTime {
public static void main(String[] args) {
long programStartTime = 0;
programStartTime = System.currentTimeMillis();
/*
* perform required task
*/
long programEndTime = System.currentTimeMillis();
long minutes = ((programEndTime - programStartTime)/1000) / 60;
long seconds = ((programEndTime - programStartTime)/1000) % 60;
System.out.println("Task performed in "+minutes+" Minutes "+seconds+" Seconds");
}
}
Try for below task!
for(int i=0; i<100000000; i++)
System.out.print("");
The output in my system was "Task performed in 0 Minutes 8 Seconds"
-- How to send an Email using Java code!
import java.io.UnsupportedEncodingException;
import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class SendEmail {
public static void main(String[] args) throws InterruptedException {
final String username = "deepak*****@gmail.com"; //supply your own userid
final String password = "********"; //supply your own password
String fromAddress = "deepak*****@gmail.com";
String toAddresses = "deepak******@gmail.com"; //,rohitvishwakarma06@gmail.com";
Properties props = new Properties();
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.port", "587");
Session session = Session.getInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
});
try {
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(fromAddress));
message.setRecipients(Message.RecipientType.TO,InternetAddress.parse(toAddresses));
message.setSubject("Test#1");
message.setText("I am glad that it is working...!");
Transport.send(message);
System.out.println("Done");
} catch (MessagingException e) {
throw new RuntimeException(e);
}
}
}
-- How to use TimerTask for cronjobs in Java
Main.java
public class Main{
public static void main(String[] args){
Timer t = new Timer();
MyTask mTask = new MyTask();
// This task is scheduled to run every 10 seconds
t.scheduleAtFixedRate(mTask, 0, 10000);
}
}
MyTask.java
class MyTask extends TimerTask{
public MyTask(){
//Some stuffs
}
@Override
public void run() {
System.out.println("Hi see you after 10 seconds");
}
}
No comments:
Post a Comment