Hi can anyone tell me how can i change a given timestamp format into other timestamp format
like
DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Calendar d1 = Calendar.getInstance();
d1.setTime(formatter.parse("2012-03-08 17:32:56"));
to
"dd-MMM-yyyy HH:mm:ss"
format
format the date with new date format dd-MMM-yyyy HH:mm:ss
DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date oldFormatedDate = formatter.parse("2012-03-08 17:32:56");
System.out.println(new SimpleDateFormat("dd-MMM-yyyy HH:mm:ss").
format(oldFormatedDate));
Continue with DateFormat#format(Date):
SimpleDateFormatter newFormat = new SimpleDateFormatter("dd-MMM-yyyy HH:mm:ss");
String newFormattedDate = newFormat.format(d1);
Related
I have a String date coming in form of dd.MM.yyyy. I want to compare if its a future date (today+1 day)
I am trying to convert the string into date and getting current date from SimpleDateFormat but when trying to convert the string date I am getting the output in "EEE MMM dd HH:mm:ss zzz yyyy" format.
String profileUpdateChangeDate = "31.01.2023"
SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy");
Date changeDate = sdf.parse(profileUpdateChangeDate);
_log.info("changeDate===>>>"+changeDate);
Date date = new Date();
SimpleDateFormat formatter = new SimpleDateFormat("dd.MM.yyyy");
String str = formatter.format(date);
_log.info("Currentdate-===>"+str);
How can I check if profileUpdateChangeDate is a future date?
You should be using the new java.time classes, so:
String profileUpdateChangeDate = "31.01.2023";
DateTimeFormatter df = DateTimeFormatter.ofPattern("dd.MM.yyyy");
LocalDate changeDate = LocalDate.parse(profileUpdateChangeDate, df);
LocalDate date = LocalDate.now();
System.out.printf("Is date %s in the future? %b%n", profileUpdateChangeDate, date.isBefore(changeDate));
You can compare the parsed date "changeDate" with the current date. If the "changeDate" is after the current date, then it is a future date.
String profileUpdateChangeDate = "31.01.2023";
SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy");
Date changeDate = sdf.parse(profileUpdateChangeDate);
Date currentDate = new Date();
if (changeDate.after(currentDate)) {
System.out.println("profileUpdateChangeDate is a future date");
} else {
System.out.println("profileUpdateChangeDate is not a future date");
}
I know that to receive date I have to use this code with dd in lowercase:
DateFormat dateFormat0 = new SimpleDateFormat("yyyy/mm/dd HH:MM:SS");
Date dateNow0 = new Date();
System.out.println("Date format: " + dateFormat0.format(dateNow0));
and the output is:
Date format: 2016/28/06 09:03:493
But when I use DD in uppercase in date format. code:
DateFormat dateFormat1 = new SimpleDateFormat("yyyy/mm/DD HH:MM:SS");
Date dateNow1 = new Date();
System.out.println("Date format: " + dateFormat1.format(dateNow1));
The output is:
Date format: 2016/28/66 09:03:494
Why I received different result using capital 'DD' instead of 'dd'?
What the result of 'DD' stands for?
D is Day in year (1-365)
d is day in month (1-31)
See the docs on SimpleDateFormat: https://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html
The proper solution code in Java 7 is:
DateFormat dateFormat3 = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Calendar cal = Calendar.getInstance();
System.out.println(dateFormat3.format(cal.getTime()));
and in Java 8:
LocalDateTime localDateTime = LocalDateTime.now();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss");
String text = localDateTime.format(formatter);
System.out.println(text);
I'd like to calculate the time difference between two dates, but I'm unable to parse a string representation of a date that looks like 2015-01-13 15:59:10, for example, through a formatter SimpleDateFormat format = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
Only string dates in the form, String dateStart = "01/14/2012 09:29:58";, for example work.
How should I go about parsing 2015-01-13 15:59:10, for example through SimpleDateFormat format = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");?
Your format needs to match the literal you're trying to parse:
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
format = new SimpleDateFormat("MM-dd-yyyy HH:mm:ss");
instead of
format = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
Your format must match your input string. The correct format is:
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
See the documentation for more details and an explanation.
to parse 2015-01-13 15:59:10 you should change the date format to below:
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
I have my date and time stored in string i.e my string contains str ="18/01/2013 5:00:00 pm". How can I convert it to 24 format time in android?
You can use two SimpleDateFormat instances: one to parse the input as a date and a second to format the date as a string with the desired format.
For example, formattedDate in the code below will be 18/01/2013 17:00:00:
String str = "18/01/2013 5:00:00 pm";
SimpleDateFormat input = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss a");
Date dt = input.parse(str);
SimpleDateFormat output = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
String formattedDate = output.format(dt); //contains 18/01/2013 17:00:00
Notes:
hh is for Hour in am/pm (1-12) whereas HH is for Hour in day (0-23).
for more formatting options, check the javadoc
try
String str ="18/01/2013 5:00:00 pm";
Date date = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss a").parse(str);
str = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss").format(date);
System.out.println(str);
output
18/01/2013 17:00:00
To get AM PM and 12 hour date format use hh:mm:ss a as string formatter WHERE hh is for 12 hour format and a is for AM PM format.
Note: HH is for 24 hour and hh is for 12 hour date format
SimpleDateFormat formatter = new SimpleDateFormat("mm/dd/yyyy hh:mm:ss a");
String newFormat = formatter.format(testDate);
Example
String date = "18/01/2013 5:00:00 pm";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/mm/dd HH:MM:SS");
Date testDate = null;
try {
testDate = sdf.parse(date);
}catch(Exception ex){
ex.printStackTrace();
}
SimpleDateFormat formatter = new SimpleDateFormat("mm/dd/yyyy hh:mm:ss a");
String newFormat = formatter.format(testDate);
System.out.println(".....Date..."+newFormat);
You can use SimpleDateFormat for that:
SimpleDateFormat dateFormat = new SimpleDateFormat(dd/mm/yyyy HH:mm:ss");
Refer to this which was opposite to your requirement. Just posted the link so you might get the idea of difference that HH and hh makes.
Try using the java SimpleDateFormat class.
Example:
SimpleDateFormat df = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
df.parse(date);
The upper case HH use a 24h format
Hi I have the following date as String format.
Input
2010-04-20 05:34:58.0
Output I want the string like
20, Apr 2010
Can someone tell me how to do it ?
Try this:
DateFormat inputFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S");
DateFormat outputFormat = new SimpleDateFormat("dd, MMM yyyy");
Date yourDate = inputFormat.parse("2010-04-20 05:34:58.0");
String formattedDate = outputFormat.format(yourDate);
You might want to try this:
SimpleDateFormat inFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S");
Date date = inFormat.parse( "2010-04-20 05:34:58.0");
SimpleDateFormat outFormat = new SimpleDateFormat("dd, MMM yyyy");
System.out.println(outFormat.format( date));