How to convert est datetime String into GMT datetime in java [duplicate] - java

This question already has an answer here:
Converting the format of the date in java
(1 answer)
Closed 1 year ago.
I will be reciving the Input in this format 201201 , which is YYYYMM format .
Now i want to return the value 201201 as it is , but it it should be in a java.util.Date format
I am confused
String strDate = "201201";
SimpleDateFormat sdFormat = new SimpleDateFormat("YYYYMM");
Now i am not able to return in the java.util.Date format with the value as 201201
I ahve edited the question it must be in YYYYMM format .
I tried this way
public class StringToDate {
public static void main(String[] args) {
DateFormat df = new SimpleDateFormat("yyyymm");
try {
Date today = df.parse("201201");
System.out.println(df.format(today));
//System.out.println("Today = " + df.format(today));
} catch (ParseException e) {
e.printStackTrace();
}
}
}

EDIT: mm is minute MM is month
Try : DateFormat df = new SimpleDateFormat("yyyyMM");
Then use the parse method:
http://docs.oracle.com/javase/1.4.2/docs/api/java/text/DateFormat.html#parse%28java.lang.String%29

The only thing I see off is your SimpleDateFormat declaration. It should be
DateFormat df = new SimpleDateFormat("yyyyMM");
Then, System.out.println(df.format(today)); will return today as "201201"
You'll need to use SimpleDateFormat to get any java.util.date into the desired format you want.
A java.util.date without formatting outputs like "Sun Jan 01 00:00:00 EST 2012" as a String.
A java.util.date object can't be set natively with a format of "yyyyMM".

SimpleDateFormat df = new SimpleDateFormat("yyyymm"); should be SimpleDateFormat df = new SimpleDateFormat("yyyyMM");
MM = Month in year
mm = Minute in hour

Related

Wrong date conversion [duplicate]

This question already has answers here:
Java SimpleDateFormat always returning January for Month
(4 answers)
Closed 5 years ago.
I have a problem in date conversion in my android application.
I have date strings like 2017-11-11 11:52 which its date is equal to 2017-Nov-11 but it is parsed as 2017-01-11 in below code snippet:
DateFormat df = new SimpleDateFormat("yyyy-MM-DD HH:mm");
try {
Date date = df.parse("2017-11-11 11:52");
Log.v("DATE_TAG","Date Time:"+date.toString());
} catch (ParseException e) {
e.printStackTrace();
}
The log output of above code is "Wed Jan 11 11:52:00 GMT+03:30 2017".
Is there anything wrong in my date format string?
You are using a wrong dateformat.
DD stands for the day of the year, not the day of the month. You have to use dd instead.
You can check the SimpleDateFormat documentation, where it is stated that DD can range from 1 to 365.
So your code should look like this:
DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm");
try {
Date date = df.parse("2017-11-11 11:52");
Log.v("DATE_TAG","Date Time:"+date.toString());
} catch (ParseException e) {
e.printStackTrace();
}
D is day in year e.g 189. Use d instead
Clearly noted from your output : Read Document
D is Day in year (1-365)
d is day in month (1-31)
Change this
DateFormat df = new SimpleDateFormat("yyyy-MM-DD HH:mm");
to
DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm");
D is used for (Day in year), so in you case you need to use d (is day in month ). please use this Format "yyyy-MM-dd HH:mm"

Create a function that takes string object and returns Date object in dd-MM-yyyy format [duplicate]

This question already has answers here:
display Java.util.Date in a specific format
(11 answers)
Closed 6 years ago.
I have date in string object. I want to convert into Date object.
Date getDateFmString(String dateString)
{
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date convertedCurrentDate = sdf.parse(dateString);
return convertedCurrentDate ;
}
above function returning following output.
Fri Apr 22 00:00:00 IST 2016
but I want output in this format '2016-03-01' only
function should take string only.
function should return Date object.
I have done lot of research over web, but I got solution from one Expert.
Date getDateFrmString(String dDate)
{
java.sql.Date dDate = new java.sql.Date(new SimpleDateFormat("yyyy-MM-dd").parse(sDate).getTime());
return dDate;
}
this is what I want.
Change the date format from
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
to
SimpleDateFormat sdf = new SimpleDateFormat("dd-mm-yyyy");
Hope this works
See this example
public Class DateFormatDemo{
public static void main (String args[]) {
SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy);
String dateInString = "01/01/2015";
try{
Date date = formatter.parse(dateInString);
System.out.println(date);
System.out.println(formatter.format(date));
}catch(ParseException e){
e.printStackTrace();
}
}
}
This link might help you with string to date object conversions
You are parsing with the wrong format try
String dateString="01-01-2016";
SimpleDateFormat sdfP = new SimpleDateFormat("dd-MM-yyyy");
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date convertedCurrentDate = sdfP .parse(dateString);
String date=sdf.format(convertedCurrentDate );
System.out.println(date);
Output:
2016-01-01
DEMO1
And if you want the format to dd-MM-yyyy then no need to define seperate SimpleDateFormat object.
String dateString="01-01-2016";
SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
Date convertedCurrentDate = sdf.parse(dateString);
String date=sdf.format(convertedCurrentDate );
System.out.println(date);
OUTPUT:
01-01-2016
DEMO2
To format the string date you have to first parse the String to Date object using the same format of date which the String have then format it using the desired format as seen in the above code.
Date objects don't have a format. Only a String does. A Date object will be output with whatever format you tell it to be format as. It all depends on what the format of the DateFormat object is when you call .format(). Calling the toString() method on a Date object uses a DateFormat of "dow mon dd hh:mm:ss zzz yyyy".
Let's do it step by step:
You have a date as String in dd-MM-yyyy format.
You want to convert it into date. (for this you are using SimpleDateFormat)
Now you are printing the date. Question here is are you printing the converted date object or input string?
If its a date object then toString method is called of date class.
As per comment on java.util.Date class it's:
dow mon dd hh:mm:ss zzz yyyy
similar to
Fri Apr 22 00:00:00 IST 2016
So that coincides with what you get in output in the second approach. But how is that code even running is strange.
String inputStr = "11-11-2012";
DateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");
Date inputDate = dateFormat.parse(input);
Variable 'input' is not defined.
What are the possible solutions:
While printing date, convert it back to String using SimpleDateFormat as per the requirement.
Date d =new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String dStr = sdf.format(dateString);
System.out.printn(dStr);
Extending class java.util.Date and override toString, but that would be a bad idea.

Java- milliseconds (SSS) not being parsed with SimpleDateFormat [duplicate]

This question already has answers here:
Java date is not preserving milliseconds in conversion with simple date format
(5 answers)
Closed 7 years ago.
Basically I'm attempting to parse date/time to Java, but having issues when trying to parse the milliseconds.
Example of data to be parsed: a[0] = 16/03/2015, a[1] = 10:00:18.120
I read in the two values and concatenate them.
Getting: dateTime = (java.lang.String) "16/03/2015 10:00:18.120"
As you can see the string has the milliseconds when i debug it. From here I parse it to SimpleDateFormat. It works- however the milliseconds are not displayed
DateFormat df = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss.SSS", Locale.ENGLISH);
String dateTime;
dateTime= a[0]+" "+a[1];
Date d = df.parse(dateTime);
Current output: d = (java.util.Date) Mon Mar 16 10:00:18 GMT 2015
Thanks for your help.
Your code is fine, but not your interpretation of the result. As correctly mentioned in one comment, the method toString() of class java.util.Date does not output the millisecond part. But the millisecond part is still part of the state of your result object. Proof:
SimpleDateFormat df = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss.SSS", Locale.ENGLISH);
String dateTime = "16/03/2015 10:00:18.120";
Date d = df.parse(dateTime);
System.out.println(d); // Mon Mar 16 10:00:18 CET 2015
System.out.println(d.getTime()); // 1426496418120
System.out.println("millisecond-part=" + (d.getTime() % 1000)); // millisecond-part=120
So all is fine. You can even format your result back to a string using the same (or another instance of SimpleDateFormat - maybe with different pattern, locale and timezone).
If java.util.Date was correctly implemented as value-type then the inventors of that class would have taken care of making the output of toString() representing the whole exact state of the object but it has not happened - another example why this class is broken.
Using DateFormat.format(Date date) function might meet your requirement
Date date = new Date();
DateFormat df = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss.SSS",Locale.ENGLISH);
String dateTime;
dateTime=df.format(date);
String[] a=dateTime.split(" ");
System.out.println(a[0]+" "+a[1]);
DateFormat df = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss.SSS",Locale.ENGLISH);
String dateTime;
dateTime= "03/16/2015"+" "+"10:00:18.120";
Date d = df.parse(dateTime);
System.out.println(df.format(d));
Try this:
String[] a = new String[]{"16/03/2015", "10:00:18.120"};
DateFormat df = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss.SSS", Locale.ENGLISH);
String dateTime = a[0] + " " + a[1];
try {
Date d = df.parse(dateTime);
System.out.println(d.getTime());//Returns milliseconds
} catch (ParseException ex) {
Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
}
The result: 1426492818120

Java how to make date from string [duplicate]

This question already has answers here:
Java string to date conversion
(17 answers)
Closed 8 years ago.
how can i make Date data type from String in java?
input: String "2014/01/01 18:02:02" => output: Date 2014/01/01 18:02:02
You could use a DateFormat to parse and format (they're reciprocal functions) the String. Something like,
String str = "2014/01/01 18:02:02";
DateFormat df = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
try {
Date d = df.parse(str);
System.out.println(df.format(d));
} catch (ParseException e) {
e.printStackTrace();
}
Output is (as requested)
2014/01/01 18:02:02
please use SimpleDateFormat to parse String To Date.
In there you can find suitable DateFormat to convert this String to Date.
you can try this
String dateString = "2014/01/01 18:02:02";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss");
LocalDateTime StringAsDate = LocalDateTime.parse(dateString, formatter);
I would recommend using Time(java.time) API instead of java.util for your Date & Time needs as it is new and exclusively added to java for Date & Time operations.
You can use the following code snippet -
String dateString = "10-11-2014";
DateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");
Date inputDate = dateFormat.parse(dateString);
You can try this too.
SimpleDateFormat formatter = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
String dateInString = "2014/01/01 18:02:02";
Date date = formatter.parse(dateInString);
System.out.println(date);
System.out.println(formatter.format(date));
It is a working and it is also suitable to you as it is not complicated. you will get this output:
Wed Jan 01 18:02:02 IST 2014
2014/01/01 18:02:02
For your date following code should work :-
String myString = "2014/01/01 18:02:02";
DateFormat myDateFormat = new SimpleDateFormat("yyyy/MM/dd hh:mm:ss");
Date myDate = myDateFormat.parse(myString);
For more details, see the documentation for SimpleDateFormat. Just make sure you use capital "MM" for month and small "mm" for minute.

Java : Return a String in the java.util.Date Format [duplicate]

This question already has an answer here:
Converting the format of the date in java
(1 answer)
Closed 1 year ago.
I will be reciving the Input in this format 201201 , which is YYYYMM format .
Now i want to return the value 201201 as it is , but it it should be in a java.util.Date format
I am confused
String strDate = "201201";
SimpleDateFormat sdFormat = new SimpleDateFormat("YYYYMM");
Now i am not able to return in the java.util.Date format with the value as 201201
I ahve edited the question it must be in YYYYMM format .
I tried this way
public class StringToDate {
public static void main(String[] args) {
DateFormat df = new SimpleDateFormat("yyyymm");
try {
Date today = df.parse("201201");
System.out.println(df.format(today));
//System.out.println("Today = " + df.format(today));
} catch (ParseException e) {
e.printStackTrace();
}
}
}
EDIT: mm is minute MM is month
Try : DateFormat df = new SimpleDateFormat("yyyyMM");
Then use the parse method:
http://docs.oracle.com/javase/1.4.2/docs/api/java/text/DateFormat.html#parse%28java.lang.String%29
The only thing I see off is your SimpleDateFormat declaration. It should be
DateFormat df = new SimpleDateFormat("yyyyMM");
Then, System.out.println(df.format(today)); will return today as "201201"
You'll need to use SimpleDateFormat to get any java.util.date into the desired format you want.
A java.util.date without formatting outputs like "Sun Jan 01 00:00:00 EST 2012" as a String.
A java.util.date object can't be set natively with a format of "yyyyMM".
SimpleDateFormat df = new SimpleDateFormat("yyyymm"); should be SimpleDateFormat df = new SimpleDateFormat("yyyyMM");
MM = Month in year
mm = Minute in hour

Categories