I want to convert 3/13/2014 11:38:58 AM string to date format.
I see some examples but and also implement but I don't know how to convert AM/PM to 24 hour time format.
How to make it possible ?
Use SimpleDateFormat
Date date = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss a").parse(string);
Using this you can convert your date and time..
SimpleDateFormat formatter = new SimpleDateFormat("mm/dd/yyyy hh:mm:ss a");
Date date_current = new Date();
Date date_start = null;
date_start = sdf.parse("3/13/2014 11:38:58 AM");
System.out.println("now time is.." + date_start);
Thanks..
Parsing Strings into Dates:
The SimpleDateFormat class has some additional methods, notably parse( ) , which tries to parse a string according to the format stored in the given SimpleDateFormat object. For example:
import java.util.*;
import java.text.*;
public class DateDemo {
public static void main(String args[]) {
SimpleDateFormat ft = new SimpleDateFormat ("yyyy-MM-dd");
String input = args.length == 0 ? "1818-11-11" : args[0];
System.out.print(input + " Parses as ");
Date t;
try {
t = ft.parse(input);
System.out.println(t);
} catch (ParseException e) {
System.out.println("Unparseable using " + ft);
}
}
}
Related
I’m trying to calculate the number of days between 2 dates. When I run this, it throws the catch (ParseException ex).
import java.text.SimpleDateFormat;
import java.text.ParseException;
import java.util.Date;
import java.util.concurrent.TimeUnit;
public class Main {
public static void main(String[] args) {
String date1 = "11/11/2020";
String date2 = "13/11/2020";
try {
SimpleDateFormat dateFormat = new SimpleDateFormat("dd-mm-yyyy");
Date date_1 = dateFormat.parse(date1);
Date date_2 = dateFormat.parse(date2);
System.out.println(date_1);
System.out.println(date_2);
long numberOfDays = date_2.getTime() - date_1.getTime();
numberOfDays = TimeUnit.DAYS.convert(numberOfDays, TimeUnit.MILLISECONDS);
System.out.println(numberOfDays);
}
catch (ParseException ex)
{
System.out.println("error");
}
}
}
other than the catch, there are no errors, so I’m kind of lost.
Don't use Date. Try this.
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("dd/MM/yyyy");
String date1 = "11/11/2020";
String date2 = "13/11/2020";
LocalDate d1 = LocalDate.parse(date1,dtf);
LocalDate d2 = LocalDate.parse(date2,dtf);
long ndays = d1.datesUntil(d2).count();
System.out.println(ndays);
If you had printed the catched exception:
System.out.println("error: " + ex.getLocalizedMessage());
You would have seen:
error: Unparseable date: "11/11/2020"
The problem is in:
SimpleDateFormat dateFormat = new SimpleDateFormat("dd-mm-yyyy");
change it to:
SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
Since the provided dates are in that format.
If Java 8 is an option I'd recommend using the Time API.
Example:
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
public class Main
{
public static void main(String[] args) {
DateTimeFormatter format = DateTimeFormatter.ofPattern("dd/MM/yyyy");
String date1 = "11/11/2020";
String date2 = "13/11/2020";
LocalDate firstDate = LocalDate.parse(date1, format);
LocalDate secondDate = LocalDate.parse(date2, format);
long days = ChronoUnit.DAYS.between(firstDate, secondDate);
System.out.println("Days between: " + days);
}
}
Just change this :
SimpleDateFormat dateFormat = new SimpleDateFormat("dd-mm-yyyy");
to that :
SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
The date you are trying to parse 11/11/2020 does not match the date format you are trying to use dd-mm-yyyy
You can resolve problems like that on your own by printing out the stack trace inside catch :
ex.printStackTrace();
First, you have different formats in input dates and defined format. Therefore, you're getting a parsing exception.
Secondly, We can java.time.Duration class in Java8 for such calculation. Example:
public static void main(String[] args) throws ParseException {
String date1 = "11/11/2020";
String date2 = "13/11/2020";
DateFormat format = new SimpleDateFormat("dd/MM/yyyy");
Duration duration = Duration.between(format.parse(date1).toInstant(), format.parse(date2).toInstant());
System.out.println("Days between: " + duration.toDays());
}
Your pattern is incorrect.
You use:
new SimpleDateFormat("dd-mm-yyyy");
but you need use:
new SimpleDateFormat("dd/mm/yyyy");
because yours date's have "/" instead of "-"
I am getting some JSON data from server that includes dates too. But it shows the date like this 2017-07-20 00:00:00 but I want to just see the date like this:2017-07-20, and i checked the previous questions about this issue but all of them were based on the date in the android side. And the problem is that I get the date as JSON and because of that I don't know how to remove Time from it.
Did you try to simple parse this string like this?
String date_string = "2017-07-20 00:00:00";
String[] parsed = date_string.split(" ");
String your_wanted_string = parsed[0];
System.out.println(your_wanted_string);
EDIT
You have to convert string into Date like here : https://stackoverflow.com/a/4216767/1979882
Convert Date to milliseconds. Or use Calendar class.
Calculate the difference between the values.
An example:
http://www.mkyong.com/java/how-do-get-time-in-milliseconds-in-java/
public class TimeMilisecond {
public static void main(String[] argv) throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat("dd-M-yyyy hh:mm:ss");
String dateInString = "22-01-2015 10:20:56";
Date date = sdf.parse(dateInString);
System.out.println(dateInString);
System.out.println("Date - Time in milliseconds : " + date.getTime());
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
System.out.println("Calender - Time in milliseconds : " + calendar.getTimeInMillis());
}
}
String date_from_json="your date goes here";
parseDate(date_from_json);
public String parseDate(String s) {
SimpleDateFormat inputFormat = new SimpleDateFormat("yyyy-mm-dd hh:mm:ss");
SimpleDateFormat outputFormat = new SimpleDateFormat("yyyy-mm-dd");
Date date = null;
String str = null;
try {
date = inputFormat.parse(s);
str = outputFormat.format(date);
} catch (ParseException e) {
e.printStackTrace();
}
return str;
}
You can use my javascript function to do this task from client side:
function formatDate(dateString) {
var date = new Date("2017-07-20 00:00:00"),
dd = date.getDate(),
mm = date.getMonth() + 1,
yyyy = date.getFullYear();
mm = mm < 10 ? '0' + mm : mm;
return dd + '-' + mm +'-' + yyyy;
}
call:
var dateStr = formatDate("2017-07-20 00:00:00");
demo
This question already has answers here:
Y returns 2012 while y returns 2011 in SimpleDateFormat
(5 answers)
Closed 5 years ago.
I am trying to parse string into a date using the following code:
public static Date dateFormatter(String s)
{
SimpleDateFormat ft = new SimpleDateFormat ("MMddYYYY");
Date excelDate=null;
try
{
excelDate = ft.parse(s);
Date formatString = ft.format(excelDate);
System.out.println("Date to be printed in Excel is :" +formatString);
return excelDate;
}
catch(Exception ae)
{
System.out.println("No date");
}
return excelDate;
}
I am passing in the argument "04202017".
This function is not working for me. I am not able to figure out what I am doing wrong. Can anybody please help me?
You have to use ft.parse(s); instead of format(excelDate). Format is the other way (Date -> String)
DateFormat.parse(String)
And you dont have to parse the Date back to a String.
Corrected code:
public static Date dateFormatter(String s) {
SimpleDateFormat ft = new SimpleDateFormat ("MMddYYYY");
Date excelDate = null;
try {
excelDate = ft.parse(s);
System.out.println("Date to be printed in Excel is :" +excelDate);
return excelDate;
} catch(Exception ae) {
System.out.println("No date");
}
return excelDate;
}
You already parsed String s to excelDate with date format that you want. So i think it's good and enough to print just excelDate.
System.out.println("Date to be printed in Excel is :" +excelDate);
Like that.
And also change MMddYYYY to MMddyyyy.
Try parse method instead of format
For String to Date, use:
SimpleDateFormat.parse(String);
For Date to String, use:
SimpleDateFormat.format(date);
However, in your code, you already parsed the String and assigned into excelDate on this line:
excelDate = ft.parse(s);
try this one:
String string = "march 9, 2017";
DateFormat format = new SimpleDateFormat("MMMM d, yyyy", Locale.ENGLISH);
Date date = format.parse(string);
System.out.println(date);
It would be nice to use Java 1.8's new time classes (which are in java.time.* package).
public static void main(String[] args)
{
LocalDateTime dateTime = LocalDateTime.now();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
// To String
String dateString = dateTime.format(formatter);
System.out.println(dateString);
// To LocalDateTime
LocalDateTime parsedLocalDateTime = LocalDateTime.parse(dateString, formatter);
}
I am trying to use SimpleDateFormat.parse method to parse a date string to Date object, but it is omitting "T" in the final date that is returned. I am passing this date string 2015-04-15T12:55:07.365 and I am getting 2015-04-15 12:55:07.365 in the output. However, the desired output is 2015-04-15T12:55:07.365.
Why is "T" in the final output omitted by this line parsedDate = sdf.parse(transDate);
public static void main(String[] args)
{
try
{
final String pattern = "yyyy-MM-dd'T'hh:mm:ss.SSS"; // example 2015-04-15T12:55:07.365
final SimpleDateFormat sdf = new SimpleDateFormat(pattern);
String transDate = "2015-04-15T12:55:07.365";
Date parsedDate = sdf.parse(transDate);
System.out.println("transDate:"+transDate+", parsedDate: "+parsedDate);
}
You never get your desired output 2015-04-15T12:55:07.365
Why?
Because you are printing Date object parsedDate.Date class has it's own toString() method implementation.When you are printing the date object, it means it basically prints the toString() method implementation format.
see the Java doc for details
System.out.println(parsedDate) would give you Wed Apr 15 00:55:07 GMT 2015 which is the toString() representation of the date object.
You can use SimpleDateFormat to parse AND format dates:
SimpleDateFormat sdfParser = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss.SSS");
Date date = sdfParser.parse("2015-04-15T12:55:07.365");
SimpleDateFormat sdfFormatter = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss.SSS");
String formattedDate = sdfFormatter.format(date);
System.out.println(formattedDate);
// 2015-04-15T12:55:07.365
You will get desired output here.
public static void main(String args[]) {
{
try {
String transDate = "2015-04-15T12:55:07.365";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss.SSS");
Date date = sdf.parse(transDate);
SimpleDateFormat output = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
Date d = sdf.parse(sdf.format(date));
String formattedTime = output.format(d);
System.out.println("transDate:" + transDate + ", parsedDate: " + formattedTime);
} catch (Exception e) {
}
}
}
I have a string with six numbers: 650310. It represents 1965 march 10 in YYMMDD format.
Is there any method to recognize this format to 10 march 1965?
Currently this is my method of doing which isn't very effective.
public class Example {
public static void main(String args[]) {
//date in YYMMDD
//String x = "650310";
String x = "161020";
System.out.print(x.substring(4, 6)+" ");
if (Integer.parseInt(x.substring(2, 4)) == 10) {
System.out.print("October"+" ");
}
else if (Integer.parseInt(x.substring(2, 4)) == 03) {
System.out.print("March"+" ");
}
if (Integer.parseInt(x.substring(0, 2)) > 50) {
String yr = "19" + x.substring(0, 2);
System.out.println(yr);
} else if (Integer.parseInt(x.substring(0, 2)) < 50) {
String yr = "20" + x.substring(0, 2);
System.out.println(yr);
}
}
}
output : 20 October 2016
Use Java's SimpleDateFormat:
SimpleDateFormat inFormat = new SimpleDateFormat( "yyMMdd" );
Date theDate = format.parse( "650310" );
Now you have a Date object which you can use to display the date in other formats:
SimpleDateFormat outFormat = new SimpleDateFormat( "dd MMMMM yyyy" );
StringBuffer output = outFormat.format( theDate );
Use output.toString() to display your newly formatted date. Good luck.
try this example
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Example {
public static void main(String args[]) throws ParseException {
SimpleDateFormat s = new SimpleDateFormat( "yyMMdd" );
Date theDate = s.parse( "650310" );
SimpleDateFormat p = new SimpleDateFormat( "dd MMMMM yyyy" );
System.out.println(p.format(theDate));
}
}
OUTPUT 10 March 1965
This link will help. Create a SimpleDateFormat object and use it to parse Strings to Date and to format Dates to Strings.
Use SimpleDateFormat for date parsing. For example:
SimpleDateFormat format = new SimpleDateFormat("yyMMdd");
try {
System.out.println(format.parse("900310"));
} catch (ParseException e) {
e.printStackTrace();
}
Output: Sat Mar 10 00:00:00 MSK 1990
EDIT: if you want to parse date,try to use DateFormat to get Date !!!! And then you can format it in your own way. I disagree with your downvote.
SimpleDateFormat format = new SimpleDateFormat("yyMMdd");
try {
Date parse = format.parse("900310");
format.applyPattern("dd MMMM yyyy");
System.out.println(format.format(parse));
} catch (ParseException e) {
e.printStackTrace();
}
output 10/Март/1990