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; } } |
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
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();
}
}
}
|
OutputDev - 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()
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
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:
Syntax:
Here is the syntax of this method:
Parameter
Returns
Throws
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