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

Wednesday, January 4, 2017

How to get Yester day Date


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
package com.javasunderesan;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class FindYesterDayDate {

 public static void main(String[] args) {
  FindYesterDayDate fydd = new FindYesterDayDate();
  System.out.println(fydd.getYesterdayDateString());
 }

 public String getYesterdayDateString() {
  String prevDate = null;
  Calendar cal = null;
  SimpleDateFormat datetimeFormatYYYYMMDD = new SimpleDateFormat("yyyy-MM-dd");
  String currDate = datetimeFormatYYYYMMDD.format(new Date());
  try {
   cal = Calendar.getInstance();
   cal.setTime(datetimeFormatYYYYMMDD.parse(currDate));
   cal.add(Calendar.DATE, -1);
   prevDate = datetimeFormatYYYYMMDD.format(cal.getTime());
  } catch (Exception e) {
   e.printStackTrace();
  } finally {
   cal = null;
  }
  return prevDate;
 }
}

Monday, August 8, 2016

Write a program for a String must contain three digit using String.format()

Write a program for a String must contain three digit using String.format()

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
      package com.javasunderesan;

public class StringThreeDigit {

 public static void main(String[] args) {
  try
  {
   int no = 0;
   String formattingString = null;
   for (int i = 0; i < 10; i++) {
    formattingString = String.format("Dev -  %03d", no);
    System.out.println(formattingString);
    no++;
   }
  }
  catch(Exception e)
  {
   e.printStackTrace();
  }
 }
}
Output
Dev -  000
Dev -  001
Dev -  002
Dev -  003
Dev -  004
Dev -  005
Dev -  006
Dev -  007
Dev -  008
Dev -  009

Write a Program to Add spaces before and After a String using String.format()

Write a Program to Add spaces before and After a String using String.format()
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
      package com.javasunderesan;

public class AddSpaceBeforeAndAfterString {
 public static void main(String[] args) {
  try {
   String msg = "java";
   System.out.println(String.format("%8s", msg)); // adding spaces
               // before
               // string ;
   System.out.println(String.format("%-8s", msg));// adding spaces
               // after
               // string
   // 8 is a number of characters String and spaces

  } catch (Exception e) {
   e.printStackTrace();
  }
 }
}

Output::

    java
java    

Wednesday, July 27, 2016

How to do split the String using more than one delimiter

How to do split the String using more than one delimiter

Split the String using more than one delimiter

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
       package com.javasunderesan;

public class SplitStringDelimitters {

 public static void main(String[] args) {

  String str = "name Java Sunderesan field computer engineering grade 9.98";
  try {
   String[] splits = str.split("name |field |grade ");
   for (int i = 1; i < splits.length; i++) {
    System.out.println(splits[i]);
   }
  } catch (Exception e) {
   e.printStackTrace();
  }

 }

}

Output::
Java Sunderesan
computer engineering
9.98


Java - String split() Method

Description:

This method has two variants and splits this string around matches of the given regular expression.


Syntax:
Here is the syntax of this method:

public String[] split(String regex, int limit)
or
public String[] split(String regex)


Parameter

regex : regular expression to be applied on string.

limit : limit for the number of strings in array. If it is zero, it will returns all the strings matching regex.Parameter


Returns

array of strings



Throws

PatternSyntaxException if pattern for regular expression is invalid



Tuesday, July 26, 2016

How to do Convert the date from one format to another date object in another form Program

how do convert the date from one format to-another date object in another form in java

convert-the-date-from-one-format-to-another-date-object-in-another-form Program

   package com.javasunderesan;

import java.text.SimpleDateFormat;
import java.util.Date;

public class ConvertDateObject {

 public static void main(String[] args) {
  SimpleDateFormat ddmmyyyy = new SimpleDateFormat("dd/mm/yyyy");
  SimpleDateFormat datetimeFormatyyyymmdd = new SimpleDateFormat("yyyy-mm-dd");
  try {
   ConvertDateObject cdo = new ConvertDateObject();
   String fromDate = "26/05/2016";
   System.out
     .println("Converted Date Time::" + cdo.convertDateType(fromDate, ddmmyyyy, datetimeFormatyyyymmdd));

  } catch (Exception e) {
   e.printStackTrace();
  }

 }

 private String convertDateType(String date, SimpleDateFormat orgFormat, SimpleDateFormat targetFormat) {
  String formattedDate = null;
  try {
   Date calDate = orgFormat.parse(date);
   formattedDate = targetFormat.format(calDate); // 20120821
  } catch (Exception e) {
   e.printStackTrace();
  }
  return formattedDate;
 }

}

Output:: Converted Date Time::2016-05-26

parse() and format() method used to convert One date object to another date Object

public final String format(Date date)

Formats a Date into a date/time string.
Parameters:
date - the time value to be formatted into a time string.
Returns:
the formatted time string.

Parsing a Date

Parse()

Monday, July 25, 2016

How to calculate difference between two date String in Java

How to calculate difference between two date String in Java

Parsing Date

public Date parse(String source) throws ParseException

Parses text from the beginning of the given string to produce a date. The method may not use the entire text of the given string.
See the parse(String, ParsePosition) method for more information on date parsing.
Parameters:
source - A String whose beginning should be parsed.
Returns:
A Date parsed from the string.
Throws: ParseException - if the beginning of the specified string cannot be parsed.
   package com.javasunderesan;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class CompareDateTime {

 public static void main(String[] args) {
  SimpleDateFormat sdf = new SimpleDateFormat("dd/mm/yyyy");  
  
  Date start = null, end = null;
  String fromDate = null;
  String toDate = null;
  try {
   CompareDateTime compareDate = new CompareDateTime();
   fromDate = "24/05/2016";
   toDate = "25/05/2016";
   start = sdf.parse(fromDate);
   end = sdf.parse(toDate);   
   System.out.println("Days Between " + start + " and " + end + ":::" +compareDate.daysBetween(start, end));
  } catch (ParseException e) {
    e.printStackTrace();
  }

 }

 private Long daysBetween(Date d1, Date d2) {
  return (Long) ((d2.getTime() - d1.getTime()) / (1000 * 60 * 60 * 24));
 }
}

output: Days Between Sun Jan 24 00:05:00 IST 2016 and Mon Jan 25 00:05:00 IST 2016:::1