What is the simplest way to convert 23-Mar-2011 to 23-03-2011 in Java?
Thanks everyone. This seem to solve the issue:
try {
Calendar cal = Calendar.getInstance();
cal.setTime(new SimpleDateFormat("dd-MMM-yyyy").parse("24-Nov-2002"));
SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
System.out.println(sdf.format(cal.getTime()));
} catch (ParseException e) { e.printStackTrace(); }
Try this
String strDate="23-Mar-2011";
SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MMM-yyyy");
try {
Date varDate=dateFormat.parse(strDate);
dateFormat=new SimpleDateFormat("dd-MM-yyyy");
System.out.println("Date :"+dateFormat.format(varDate));
}catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
Have you looked at SimpleDateFormat ?
Here's a simpler version of your code:
DateFormat shortFormat = new SimpleDateFormat("dd-MM-yyyy",Locale.ENGLISH);
DateFormat mediumFormat = new SimpleDateFormat("dd-MMM-yyyy",Locale.ENGLISH);
String s = "23-Mar-2011";
String shortDate = shortFormat.format(mediumFormat.parse(s));
System.out.println(shortDate);
SimpleDateFormat format1 = new SimpleDateFormat("dd-MMM-yyyy");
SimpleDateFormat format2 = new SimpleDateFormat("dd-MM-yyyy");
Date date = format1.parse("24-Nov-2002");
System.out.println(format2.format(date));
Related
How can i convert 20160729161455 to 29/07/2016 16:14:55?
I've tried the following but it did not work:
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
String dateID = 20160729161455;
try
{
Date date = sdf.parse(dateID);
System.out.println(date);
} catch (ParseException ex){}
Yes you can do. You need 2 SimpeDateFormat objects:
SimpleDateFormat dateParser= new SimpleDateFormat("yyyyMMddHHmmss");
SimpleDateFormat dateFormatter = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
String dateID = "20160729161455";
try
{
Date date = dateParser.parse(dateID);
System.out.println(dateFormatter.format(date));
} catch (ParseException ex){
ex.printStacktrace();
}
In first step you have to parse the value and in second you have to format it in the new format.
String RollStart_TIMESTAMP = RollStartDATE_TIME_STAMP+" "+rolStart_TIME_STAMP;
SimpleDateFormat dateFormat = new SimpleDateFormat("dd-mm-yyyy hh:mm");
Date parsedDate = null;
try {
parsedDate = dateFormat.parse(RollStart_TIMESTAMP);
long mili = parsedDate.getTime();
Log.e("VX:","TIMESTAMP"+mili);
} catch (ParseException e) {
e.printStackTrace();
}
Log.e("VX:","TIMESTAMP"+parsedDate);
Try this code, You get a right time stamp.
SimpleDateFormat dateFormat = new SimpleDateFormat("MMMM dd,yyyy HH:mm aaa");
timestamp = dateFormat.format(new Date()); // Find todays date
Log.e("Timestamp", timestamp);
Yes right there is wrong pattern yyyy-MM-hh is helps me out .
try {
String RollStart_TIMESTAMP = RollStartDATE_TIME_STAMP+" "+rolStart_TIME_STAMP;
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm");
Date timestamp = null;
timestamp = df.parse(RollStart_TIMESTAMP);
long ROLL_START_DATE_TIME_VALUE = timestamp.getTime()/1000;
Log.e("RX:","VALUE"+ROLL_START_DATE_TIME_VALUE);
} catch (Exception e) {
e.printStackTrace();
I need help parsing this string of data and time. Could any one help me fix my format or give guidance? Thanks.
2016-04-23T01:00:00Z
SimpleDateFormat year = new SimpleDateFormat("mm:dd:yyyy");
SimpleDateFormat hour = new SimpleDateFormat("hh:mm:ss");
String yearParse = year.format(Date.parse(year));
String hourParse = hour.format(Date.parse(hour));
Use this methods to get Date and Time in format you wants
For Date
public static String getDate(String date) {
SimpleDateFormat sourceFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
sourceFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
Date parsed = null;
try {
parsed = sourceFormat.parse(date);
} catch (ParseException e) {
e.printStackTrace();
}
SimpleDateFormat formatter = new SimpleDateFormat("dd MMMM yyyy");
return formatter.format(parsed);
}
For Time
public static String getTime(String date) {
SimpleDateFormat sourceFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
sourceFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
Date parsed = null;
try {
parsed = sourceFormat.parse(date);
} catch (ParseException e) {
e.printStackTrace();
}
SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss");
return formatter.format(parsed);
}
Try
SimpleDateFormat sdf1= new SimpleDateFormat("mm-dd-yyyy");
SimpleDateFormat sdf2= new SimpleDateFormat("hh-mm-ss");
String year=sdf1.format(required_date);
String hour=sdf2.format(required_date);
This question already has answers here:
convert string into date format
(5 answers)
Closed 9 years ago.
Hello i have date in this format 2013-10-31T19:00:00Z now i want to display this date into yyyy/mm/dd this format.i have used this code but its giving me exception kindly help me
String startDateString = "2013-10-31T19:00:00Z";
DateFormat df = new SimpleDateFormat("yyyy/mm/dd");
Date startDate;
try {
startDate = df.parse(startDateString);
String newDateString = df.format(startDate);
System.out.println("Date is "+newDateString);
} catch (ParseException e) {
e.printStackTrace();
}
Try this
String DateStr="2013-10-31T19:00:00Z";
SimpleDateFormat sim=new SimpleDateFormat("yyyy-MM-dd");
Date d = new SimpleDateFormat("yyyy-MM-dd").parse(DateStr);
System.out.println(sim.format(d));
output 2013-10-31
That's because you're specifically telling Java to look for a 4-digit year followed by a 2-digit month and 2-digit date. Taken straight from the docs, what you want is
"yyyy-MM-dd'T'HH:mm:ssZ"
You can use SimpleDateFormat's parse method to parse your date, and then use the format method to output it in the format that you prefer.
Try this:
String startDateString = "2013-10-31T19:00:00Z";
DateFormat inputFormat = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss'Z'");
DateFormat outputFormat = new SimpleDateFormat("yyyy/MM/dd");
Date date = inputFormat.parse(startDateString);
System.out.println("Date is " + outputFormat.format(date));
Output:
Date is 2013/10/31
You need a separate SimpleDateFormat to parse the Datestring. Try this:
String startDateString = "2013-10-31T19:00:00Z";
DateFormat originalFormat = new SimpleDateFormat("yyyy-mm-dd'T'HH:MM:SS'Z'");
DateFormat df = new SimpleDateFormat("yyyy/mm/dd");
Date startDate;
try {
startDate = originalFormat.parse(startDateString);
String newDateString = df.format(startDate);
System.out.println("Date is " + newDateString);
} catch (ParseException e) {
e.printStackTrace();
}
You need two different DateFormat. one for parse date and one for formatting date.
String startDateString = "2013-10-31T19:00:00Z";
DateFormat df = new SimpleDateFormat("yyyy/mm/dd");
DateFormat df1 = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:SS'Z'");
Date startDate;
try {
startDate =df1.parse(startDateString);
String newDateString = df.format(startDate);
System.out.println("Date is "+newDateString);
} catch (ParseException e) {
e.printStackTrace();
}
SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
String dateInString = "07/06/2013";
try {
Date date = formatter.parse(dateInString);
System.out.println(date);
System.out.println(formatter.format(date));
} catch (ParseException e) {
e.printStackTrace();
}
Here is simple example
Try this:
try {
String startDateString = "2013-10-31T19:00:00Z";
DateFormat input = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss'Z'");
DateFormat output = new SimpleDateFormat("yyyy/MM/dd");
Date date = input.parse(startDateString);
System.out.println("Date is " + output.format(date));
} catch (ParseException e) {
e.printStackTrace();
}
Pointing an another simple mistake in your code
You coded as DateFormat df = new SimpleDateFormat("yyyy/mm/dd");
here mm is for minutes , for month use MM
I am New to Android Development.How to convert the Following string(5/31/2011) in to the Date Object I tried different ways But Couldn't get any Luck??
String dateStr = "5/31/2011";
DateFormat df = new SimpleDateFormat("M/dd/yyyy");
Date date = df.parse(dateStr);
DateFormat df = new SimpleDateFormat("MM/dd/yyyy");
try {
Date today = df.parse("5/31/2011");
System.out.println("Today = " + df.format(today));
} catch (ParseException e) {
e.printStackTrace();
}
You can use the following
String time1 = "5/31/2011";
SimpleDateFormat formatter1 = new SimpleDateFormat("MM/dd/yyyy");
Date indate1 = formatter1.parse(time1);
Thanks Deepak