Showing posts with label Java Concepts. Show all posts
Showing posts with label Java Concepts. Show all posts

Java – How to get current date time – date() and calender()

1 Comment
How to get current date time with the help of date() class and calender() class,you will learn how easily you can get current date time - date() and calender() classes.Let's understand the concept very clearly with simple examples.

Java – How to get current date time – date() and calender()


Java – How to get current date time – date() and calender()


You can use Date() method to get current date and date is belongs to java.util.Date package ,as below

Example:


package com.java.practice;
import java.util.Date;

public class Currdate {

 public static void main(String[] args) {
  
  Date currentdate = new Date();
  System.out.println("The Current Date is ="+currentdate);  
 }

}

Output:

The Current Date is =Fri Feb 17 20:33:59 IST 2017


ALSO READ

EXTRACT NUMBERS FROM A STRING
CREATE A FILE IN JAVA


2.If you want to print date in different formats then you can use DateFormat class which is belongs to java.text.DateFormat and SimpleDateFormat() is belongs to java.text.SimpleDateFormat; package.Let's see below example to print Current date in specified format.

Example:


package com.java.practice;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Currdate {

 public static void main(String[] args) {
    
  DateFormat dformat = new SimpleDateFormat("dd//yyyy");
  Date sysdate = new Date();
  System.out.println("The Current Date is ="+dformat.format(sysdate));
 }

}




Output:

The Current Date is =17/02/2017


3.Using Calendar class


Date can be print by Calendar() class which is belongs to java.util.Calendar package and you can use SimpleDateFormat() method to print the Date format as below.

Example:


package com.java.practice;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date; 

public class Currdate { 
 public static void main(String[] args) { 

 DateFormat dformat = new SimpleDateFormat("dd//yyyy");    
 Calendar cdates = Calendar.getInstance();
 System.out.println("Current Date of Calendar Class is ="+dformat.format(cdates.getTime())); 
   }
}

Output:

Current Date with the help of Calendar Class is =17/02/2017

Please provide your valuable comments on this post and i hope you got enough knowledge.

How to create a file in Java

Add Comment
How to create a file in Java,while working with different files in Java it is very important to creating new files whether it could be any format to store the data.In this post you will learn how to create a files in java grammatically,please observe below simple example to create a file in Java.

How to create a file in Java


Also Read - Extract Number from a String


How to create a file in Java


For creating files in java you can use File.createNewFile() method is used to create a new file in Java language and which will return Boolean value means whether it is a true or false after creating the file in java.

Example:


package com.java.filescreateread;
import java.io.File;
import java.io.IOException;

public class CreateFiles {
public static void main(String[] args) throws IOException {
File file = new File("E:\\java\\Files\\Simplefiless.csv");
file.createNewFile();
if(file.isFile()){
System.out.println("New CSV file is created");
}else{
System.out.println("New CSV file is not created and already exists.");
}
}

}

Here in above code new file is created and as well as it will verify whether file is exists in created path or not.Please provide your comments on this post.

Java extract number from string

Add Comment
Java extract number from string using regex , you will get best solution.During working with Java programming,you are facing some situation where you need to print/get numbers from a  message or From Database tables or From Excel data etc.In order to work or extract numbers from String we are going to use regular expression and replaceAll method in Java.

Java Extract Number from String

Java extract number from string


Let's see the below example to extract number from a string using regular expressions in java

Example:


package com.practice.javs;
public class GetNumbers {
public static void main(String[] args) {

String name= "rajesh123456k";
String numerics = name.replaceAll("[^0-9]", "");
System.out.println(numerics);
}
}


As per above Java program,i have used one String which is having alphabets and numbers.In above example simply i have stored in another String i.e numeric's with replaceAll method for name string and used [^0-9] regular expression means retrieve only numbers between 0 to 9.

Output:

123456


replaceAll

Replaces each sub-string of this string which matches the given regular expressions with the given replacement([^0-9]). An invocation of replaceAll method of the form str.replaceAll(regex, repl) fields exactly the same result as per the expression.

Topics you have learned

1.How to get digits from String
2.How to use Regular Expression
3.replaceAll method in java