Letter Date or Time Component Presentation Examples
G Era designator Text AD
y Year Year 1996; 96
M Month in year Month July; Jul; 07
w Week in year Number 27
W Week in month Number 2
D Day in year Number 189
d Day in month Number 10
F Day of week in month Number 2
E Day in week Text Tuesday; Tue
a Am/pm marker Text PM
H Hour in day (0-23) Number 0
k Hour in day (1-24) Number 24
K Hour in am/pm (0-11) Number 0
h Hour in am/pm (1-12) Number 12
m Minute in hour Number 30
s Second in minute Number 55
S Millisecond Number 978
z Time zone General time zone Pacific Standard Time; PST; GMT-08:00
Z Time zone RFC 822 time zone -0800
1:
try {
String dateString = "30/09/2014";
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
Date date = sdf.parse(dateString);
long startDate = date.getTime();
} catch (ParseException e) {
e.printStackTrace();
}
2:
public static void main(String[] args) throws ParseException {
java.util.Date fecha = new java.util.Date("Mon Dec 15 00:00:00 CST 2014");
DateFormat formatter = new SimpleDateFormat("EEE MMM dd HH:mm:ss Z yyyy", Locale.US);
Date date;
date = (Date)formatter.parse(fecha.toString());
System.out.println(date);
Calendar cal = Calendar.getInstance();
cal.setTime(date);
String formatedDate = cal.get(Calendar.DATE) + "/" +
(cal.get(Calendar.MONTH) + 1) +
"/" + cal.get(Calendar.YEAR);
System.out.println("formatedDate : " + formatedDate);
}
3:
String dateStr = "Mon Jun 18 00:00:00 IST 2012";
DateFormat formatter = new SimpleDateFormat("E MMM dd HH:mm:ss Z yyyy");
Date date = (Date)formatter.parse(dateStr);
System.out.println(date);
Calendar cal = Calendar.getInstance();
cal.setTime(date);
String formatedDate = cal.get(Calendar.DATE) + "/" + (cal.get(Calendar.MONTH) + 1) + "/" + cal.get(Calendar.YEAR);
System.out.println("formatedDate : " + formatedDate);
4:
System.out.println("Date: "+ String.valueOf(new Date()));
SimpleDateFormat dt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String stringdate = dt.format(new Date());
System.out.println("String.valueOf(date): "+stringdate);
try {
Date date = dt.parse(stringdate);
System.out.println("parse date: "+ String.valueOf(date));
} catch (ParseException e) {
e.printStackTrace();
}
5:
java.util.Date date1 = new Date("11/19/2015");
SimpleDateFormat formatter = new SimpleDateFormat("MMM dd yyyy HH:mma");
String format = formatter.format(date);
System.out.println(format);
6:
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd-MM-yyyy");
Date date = new Date();
Date date1 = new Date();
try {
System.out.println("Date1: "+date1);
System.out.println("date" + date);
date = simpleDateFormat.parse("01-01-2013");
date1 = simpleDateFormat.parse("06-15-2013");
System.out.println("Date1 is:"+date1);
System.out.println("date" + date);
} catch (Exception e) {
System.out.println(e.getMessage());
}
7:
String newstr = "08/16/2011";
SimpleDateFormat format1 = new SimpleDateFormat("MM/dd/yyyy");
SimpleDateFormat format = new SimpleDateFormat("EE MMM dd hh:mm:ss z yyyy");
Calendar c = Calendar.getInstance();
c.setTime(format1.parse(newstr));
System.out.println(format.format(c.getTime()));
8:
public static String getDayFromDateString(String stringDate,String dateTimeFormat)
{
String[] daysArray = new String[] {"saturday","sunday","monday","tuesday","wednesday","thursday","friday"};
String day = "";
int dayOfWeek =0;
//dateTimeFormat = yyyy-MM-dd HH:mm:ss
SimpleDateFormat formatter = new SimpleDateFormat(dateTimeFormat);
Date date;
try {
date = formatter.parse(stringDate);
Calendar c = Calendar.getInstance();
c.setTime(date);
dayOfWeek = c.get(Calendar.DAY_OF_WEEK)-1;
if (dayOfWeek < 0) {
dayOfWeek += 7;
}
day = daysArray[dayOfWeek];
} catch (Exception e) {
e.printStackTrace();
}
return day;
}
9:
String dayOfTheWeek = (String) DateFormat.format("EEEE", date); // Thursday
String day = (String) DateFormat.format("dd", date); // 20
String monthString = (String) DateFormat.format("MMM", date); // Jun
String monthNumber = (String) DateFormat.format("MM", date); // 06
String year = (String) DateFormat.format("yyyy", date); // 2013
No comments:
Post a Comment