How to convert Date represented as a String to milliseconds? [duplicate] - java

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
how to convert java string to Date object
In my Java code I have input String like 05.10.2011 which I need to convert to milliseconds.
So:
String someDate = "05.10.2011";
I have to convert to match milliseconds format.

String someDate = "05.10.2011";
SimpleDateFormat sdf = new SimpleDateFormat("MM.dd.yyyy");
Date date = sdf.parse(someDate);
System.out.println(date.getTime());

You can use Java's SimpleDateFormat to easily convert to a Date instance.
SimpleDateFormat formatter = new SimpleDateFormat("MM.dd.yyyy"); // Month.Day.Year
Date d = formatter.parse(inputString);
long timestamp = d.getTime();

use the simpleDateFormat which takes a string and converts it to Date then call the getTime() to get the date in milliseconds format

Related

How i can get date format of any datetime string? [duplicate]

This question already has answers here:
Java string to date conversion
(17 answers)
How can I parse/format dates with LocalDateTime? (Java 8)
(11 answers)
Closed 4 years ago.
I receive this "10/1/2018, 1:27:42 PM" as date from server. Now want to convert it to SimpleDateFormat("yyyy-MM-dd HH:mm:ss"). But to do this i need to know the format of existing date string.
How i can find a correct format of "10/1/2018, 1:27:42 PM"?
You need to use SimpleDateFormat.toPattern() to get the pattern. Use this function in this way -
SimpleDateFormat sdf = new SimpleDateFormat();
// get the current date and time
Calendar cal = Calendar.getInstance();
String dateToday = sdf.format(cal.getTime());
// get the pattern used by the formatter and print it
String pattern = sdf.toPattern();
System.out.println("Pattern:"+pattern);

Java Convert given date to date format like MM/dd/yyyy [duplicate]

This question already has answers here:
converting date format
(5 answers)
Closed 6 years ago.
I have a situation where i can get only the date like 9/2/2016. From this, i need to parse and create a pattern like M/D/YYYY. From this i can convert the any input date to the above pattern M/D/YYYY.
Is there any utility/API for that in java.?
For your requirement, first you parse the input string with appropriate format. Then you do format the date. For more info refer SimpleDateFormat
SimpleDateFormat dateFormat = new SimpleDateFormat("M/d/yyyy");
Date myDate = dateFormat.parse("9/2/2016");
SimpleDateFormat dateFormat2 = new SimpleDateFormat("MM/dd/yyyy");
System.out.println(dateFormat2.format(myDate));
Output
09/02/2016
You can use SimpleDateFormat to do this.
SimpleDateFormat dateFormat = new SimpleDateFormat(MM/dd/yyyy);
https://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html

JSON date to formatted JAVA date [duplicate]

This question already has answers here:
Java: Date from unix timestamp
(11 answers)
Closed 6 years ago.
I am trying to get dd.MM.yyyy and hh:mm from 1436536800 but only the time is correct, the date is completely wrong. I don't really understand how this is supposed to work
int dt = time.getInt("start")*1000;
Date date = new Date(dt);
startDate = dateFormat.format(date);
If time.getInt("start") is a valid unix timestamp, you must add "000" to the number. Example: 1436536800 * 1000 = 1436536800000. Then you can use the timestamp to get a Date:
final Date date = new Date(Long.parseLong("1436536800000"));
SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy hh:mm");
System.out.println(sdf.format(date));
Console exit: 10.07.2015 09:00
Assuming the time is correct, it's likely the fact that you're multiplying by 1,000. When creating the date the way you are, it takes in milliseconds. Is it possible that your input is already in milliseconds? (Your current method will be ~2 minutes off if so)
Date date=new Date(1436536800);
SimpleDateFormat df2 = new SimpleDateFormat("dd.MM.yyyy");
String dateText = df2.format(date);
Date you are getting is a JSON string value. follow steps below to format it correctly.
First download Moment.js file and add it in your project.
var date1 = "1436536800"; // your long value contain in this variable.
var date2 = moment(date1).format(MMMM Do YYYY);//It will give you formatted date value.
see more formats below

Java - Converting yyyy-MM-dd'T'HH:mm:ssZ to readable dd-MM-yyyy [duplicate]

This question already has answers here:
Converting ISO 8601-compliant String to java.util.Date
(31 answers)
Closed 6 years ago.
I will have a String input in the style of:
yyyy-MM-dd'T'HH:mm:ssZ
Is it possible to convert this String into a date, and after, parsing it into a readable dd-MM-yyyy Date Object?
Yes. It can be done in two parts as follows:
Parse your String to Date object
SimpleDateFormat sd1 = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
Date dt = sd1.parse(myString);
Format the Date object to desirable format
SimpleDateFormat sd2 = new SimpleDateFormat("yyyy-MM-dd");
String newDate = sd2.format(dt);
System.out.println(newDate);
You will have to use two different SimpleDateFormat since the two date formats are different.
Input:
2015-01-12T10:02:00+0530
Output:
2015-01-12
//Parse the string into a date variable
Date parsedDate = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ").parse(dateString);
//Now reformat it using desired display pattern:
String displayDate = new SimpleDateFormat("dd-MM-yyyy").format(parsedDate);

Get Date Object with formatted date [duplicate]

This question already has answers here:
Java string to date conversion
(17 answers)
Closed 9 years ago.
I have a Date() object which always gives me date in a particular format (Comes in-built with Java). I want to get store Date in a specified format ("MMMM, dd yyyy HH:mm:ss"). I cam across one similar approach to do that-
Date date = new Date();
String dateFormat = "MMMM, dd yyyy HH:mm:ss";
SimpleDateFormat dt1 = new SimpleDateFormat(dateFormat);
dt1.format(date);
dt1.format(date) returns a formatted date in String data type. I want something similar which will return formatted date in Date data type.
I want something similar which will return formatted date in Date data type.
That is not possible. Date objects do not have a format by themselves, you cannot have a Date object in a specific format. You'll have to find another way to do what you need to do.
Your question is very unclear but I suspect what you want is parse a given String with a date into a Date object? If that is so, this should help: Java string to date conversion.
Given your example code, it would look like this:
String dateString = "June, 01 2014 08:23:51";
String dateFormat = "MMMM, dd yyyy HH:mm:ss";
Date date = new SimpleDateFormat(dateFormat, Locale.ENGLISH).parse(dateString);

Categories

Resources