How can I extract date from since time component [duplicate] - java

This question already has answers here:
How to convert currentTimeMillis to a date in Java?
(13 answers)
Closed 7 years ago.
I have to extract date in the format MM-dd-yyyy in java from the since time value. Since time is the time at which the doucment is created. For example, if since time is 1452413972759, date would be "Sun, 10 Jan 2016 08:19:32 GMT" (Calculated from http://www.epochconverter.com/) . From this, I could get date in desired format but I am unable to code for the first step i.e., converting since time to date. Can someone help me?
I tried
DateFormat df = new SimpleDateFormat("MM/dd/yyyy");
java.util.Date date = df.parse("1452320105343");
String DATE_FORMAT = "MM/dd/yyyy";
SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT);
System.out.println("Today is " + sdf.format(date));
But it gives parse exception.

Your code can't work because you're trying to parse a number of milliseconds as if it was a MM/dd/yyyy formatted date.
I would have expected the following code to work, where S represents milliseconds :
DateFormat df = new SimpleDateFormat("S");
java.util.Date date = df.parse("1452413972759");
String DATE_FORMAT = "MM/dd/yyyy";
SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT);
System.out.println("Today is " + sdf.format(date));
However it doesn't for some reason, displaying a date in the 1970 year.
Instead, the following code that parses seconds rather than milliseconds works for your needs :
DateFormat df = new SimpleDateFormat("s");
java.util.Date date = df.parse("1452413972"); // just remove the last 3 digits, or divide by 1000
String DATE_FORMAT = "MM/dd/yyyy";
SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT);
System.out.println("Today is " + sdf.format(date));
Or just follow the link from #mohammedkhan and use Date constructor rather than parsing a string :
java.util.Date date = new Date(1452413972759l);
String DATE_FORMAT = "MM/dd/yyyy";
SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT);
System.out.println("Today is " + sdf.format(date));

Related

Get current date in YYYY-MM-DD format in Java (Android) [duplicate]

This question already has answers here:
Month issue in SimpleDateFormat class
(6 answers)
Closed 5 years ago.
I am trying to get a Date object in the format YYYY-MM-DD representing the current system date. Below is my attempt:
Date todayDate = Calendar.getInstance().getTime();
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-mm-dd");
String todayString = formatter.format(todayDate);
The values are as follows:
todayDate: Mon May 15 16:24:47 GMT+01:00 2017
todayString: 2017-24-15
Having tried this a couple of times I noticed the todayString is not made up of YYYY-MM-DD but YYYY-[minutes][minutes]-DD.
How might I get the current date in the YYYY-MM-DD format?
Change this:
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-mm-dd");
to this:
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
mm stands for the minutes, while MM (capitalized) stands for the month

issue in converting the date from string to date type

below is the code I have used to add the number of days to the existing date..which gave me string output and I want that to be converted to Date format again...I have tried formating but it gave the out put -->
Date date = sdf.parse(dt);
sysout (date ) --giving me -- Mon May 05 00:00:00 PDT 2008
but I want it as YYYY-MM/DD
sdf.format(date) --Gives me 2008-05-05 which I am looking but it is a string object...but I want this to be converted to DATE type
String dt = "2008-01-01"; // Start date
System.out.println("start date "+dt);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Calendar c = Calendar.getInstance();
c.setTime(sdf.parse(dt));
c.add(Calendar.DATE, 125); // number of days to add
dt = sdf.format(c.getTime());
System.out.println("c.getTime() "+c.getTime());
System.out.println("end date "+dt);
Date date = sdf.parse(dt);
System.out.println("last but one date in DATE form -->" +date);
System.out.println("last formatted date in string form "+sdf.format(date));
You created the format right.
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
but you are using it incorrectly. you should use
sdf.format(your_unformated_date);
Here is a sample code that will convert date from String to Date type using SimpleDateFormat Class:
public static void convert()
{
String str="10:25:35";
SimpleDateFormat sdf=new SimpleDateFormat("hh:mm:ss");
System.out.println(sdf.format(str));
}

Convert String to Date [duplicate]

This question already has answers here:
Change date format in a Java string
(22 answers)
Closed 7 years ago.
From this question and its answers, I tried to convert string to date. But it seems to strange with me.
String test = "2015/01/01 11:56:00 ";
SimpleDateFormat df = new SimpleDateFormat("YYYY/MM/dd HH:mm:ss");
System.out.println(df.parse(test));
It returns
Mon Dec 29 11:56:00 ICT 2014
I have tried with other days but the results is not in the rule (i.e. it have the same distance from the input string and the output date). I am curious. Can anyone explain this for me?
First of all its yyyy not YYYY for year and try the below code to create a Date object from a String.
String test = "2015/01/01 11:56:00 ";
SimpleDateFormat df = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Date date = df.parse(test);
System.out.println(date);
The date format is case-sensitive and therefore the parsing is evaluated differently from what you expected.
Try this:
String test = "2015/01/01 11:56:00 ";
SimpleDateFormat df = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
System.out.println(df.parse(test));

convert string to date and time as am/pm format [duplicate]

This question already has answers here:
Java string to date conversion
(17 answers)
Closed 8 years ago.
string : 2014-04-25 17:03:13
using SimpleDateFormat is enough to format?
or
otherwise i will shift to any new API?
Date date = new Date(string);
DateFormat dateFormat = new SimpleDateFormat ("yyyy-MM-dd");
out.println( dateFormat.format (date));
My expected result is (India zone):
Date : 25-04-2014
Time : 05:03 PM
Remembering that Date objects have no inherent format, you need two DateFormat objects to produce the result you seek - one to parse and another to format:
String input = "2014-04-25 17:03:13";
DateFormat inputFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
DateFormat outputFormat = new SimpleDateFormat("'Date : 'dd-MM-yyyy\n'Time : 'KK:mm a");
System.out.println(outputFormat.format(inputFormat.parse(input)));
Output:
Date : 25-04-2014
Time : 05:03 PM
Note the use of quoted sequences in the format, such a "'Date : '", which is treated as a literal within the format pattern.
I custom onTimeSet() function . Send the hour and minutes to it. It will return the time with format am and pm
public static String onTimeSet( int hour, int minute) {
Calendar mCalen = Calendar.getInstance();;
mCalen.set(Calendar.HOUR_OF_DAY, hour);
mCalen.set(Calendar.MINUTE, minute);
int hour12format_local = mCalen.get(Calendar.HOUR);
int hourOfDay_local = mCalen.get(Calendar.HOUR_OF_DAY);
int minute_local = mCalen.get(Calendar.MINUTE);
int ampm = mCalen.get(Calendar.AM_PM);
String minute1;
if(minute_local<10){
minute1="0"+minute_local;
}
else
minute1=""+minute_local;
String ampmStr = (ampm == 0) ? "AM" : "PM";
// Set the Time String in Button
if(hour12format_local==0)
hour12format_local=12;
String selecteTime=hour12format_local+":"+ minute1+" "+ampmStr;
retrun selecteTime;
}
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm a");
more patterns you can find here
Try given below sample code:
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = new Date();
System.out.println(dateFormat.format(date));
//Output: 2013-05-20 10:16:44
For more functionalities on Data and Time try Joda-Time API .

How to convert java.lang.string To java.util.date in java [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
how to parse date in java?
I am getting "Mon Aug 01 00:00:00 IST 2011" (java.lang.String) as string and i need to convert it to "01-08-2011" Date(java.Util.Date) ,how to do this
use the SimpleDateFormat class
SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
to print the date. This will print the current date in predefined formatted using SimpleDateFormat
System.out.println("Current Date " + sdf.format(new Date());
String mydate = "01-08-2011";
to convert string into date
Date parseDate = sdf.parse(mydate);
Please take a look at (Simple)DateFormat's parse method.
Look at the SimpleDateFormat class in the javadocs.
Constructs a SimpleDateFormat using the given pattern and the default
date format symbols for the default locale
SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
System.out.println("Date " + sdf.format(new Date());

Categories

Resources