how to parse a string into date? [closed] - java

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
what is the format in SimpleDateFormat for parsing a string ie 07/12/2013 into a date type? I am using the following code, but desired format is not comimg.
SimpleDateFormat formatter ;
Date notBeforeDate ,notAfterDate;
formatter = new SimpleDateFormat("DD/MM/YYYY");
notBeforeDate = (Date)formatter.parse(notBeforeValue);
notAfterDate = (Date)formatter.parse(notAfterValue);

Assuming you are looking for a Java solution. Change the format string :
DateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
Date date = formatter.parse("07/12/2013");
Refer the documentation for the exact format string information.

Try this :
SimpleDateFormat formatter=new SimpleDateFormat("dd/MM/yyyy"); ;
Date notBeforeDate ,notAfterDate;
notBeforeDate =formatter.parse("Your String format Date");
notAfterDate =formatter.parse("Your String format Date");
Read More.
Hope it will help you.

Related

How to get date and time in below format in java? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
How can I get the current date and time and how can I transfer it into the following String format?
06-04-2020 05:00 PM
After that I need to add few minutes to the date? Please help!
Your answer should look something like this:
String dateTimePattern = "dd-MM-yyyy HH:mm a";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(dateTimePattern );
LocalDateTime yourDay= LocalDateTime.of(2020, 4, 6, 17, 00);
System.out.println(formatter.format(ZonedDateTime.of(yourDay, ZoneId.of("UTC-5"))));
Please look this up for more info on the available formats.
Date yourDate = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy h:m a");
String formatedDate= sdf.format(yourDate);

When i convert java.util.Date to java.sql.Date i mis the time, can i do that convergance with time? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
java.util.Date tempin = pLabIn.getDate();
System.out.println(tempin);
java.util.Date tempout = pLabOut.getDate();
java.sql.Date labIn = new java.sql.Date(tempin.getTime());
System.out.println(labIn);
java.sql.Date labOut = new java.sql.Date(tempout.getTime());
The first System.out shows "Thu Feb 27 22:50:06 PKT 2014"
and the second "2014-02-27"
So it miss time. I want to take time as well.
Use a java.sql.Timestamp instead.
The toString() implementation for java.sql.Date will only give you the date in the format year-month-day.
This is also precised in the documentation:
Formats a date in the date escape format yyyy-mm-dd.
If you want to keep the time in your print statement, you can use a SimpleDateFormat instead.
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println(sdf.format(labIn));

Regex for SimpleDateFormat string [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I have dates mainly in the forms of yyyy-MM-dd and yyyy-MM-dd hh:mm:ss. I want to pattern match yyyy-MM-dd hh:mm:ss.
Is it trivial to write an regex for this purpose? I am new to regex so would appreciate any resource tat could get me up to speed?
If you are trying to find these date notations in a String, then regexes is indeed a good choice. You could use this regex:
\b\d{4}-\d{2}-\d{2} \d{1,2}:\d{2}:\d{2}\b
See it in action here: http://rubular.com/r/qZOTsUikbo. Note: this matches "dates" like
9999-99-99 99:99:99 as well. If that is a problem for you, you could verify them after you found the potential Strings.
If you want to parse them, then use a SimpleDateFormat.
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
Date date = sdf.parse("2013-12-24 18:31:20");
You don't need to write reg expression for it.
Java already has support for it. Here is the link to follow.
http://developer.android.com/reference/java/text/SimpleDateFormat.html

Need help making a better code, this one is so ugly [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 9 years ago.
Improve this question
I have a query with the following data
85343
125242
65252
125655
185658
235758
Those are 24 hour times without the format. I tried parsing it with HHmmss to then give it the format hh:mm:ss a it kind of worked with the hours 12+ but with 12- it went to hell giving wrong times. I worked it out with the following code:
DateFormat dateFormat2 = new SimpleDateFormat("Hmmss");
DateFormat dateFormat21 = new SimpleDateFormat("HHmmss");
DateFormat dateFormat5 = new SimpleDateFormat("hh:mm:ss a");
System.out.println((Query.getString(1).length() == 5) ?
dateFormat5.format(dateFormat2.parse(Query.getString(1)))
: dateFormat5.format(dateFormat21.parse(Query.getString(1))));
it solves the problem with the time errors as I get the correct times
8:53:43AM
12:52:42PM
6:52:52 AM
12:56:55 PM
6:56:58 PM
11:57:58PM
Is there anyway to make this better?
I wouldn't use two different DateFormat for parsing but only one :
DateFormat dateFormat21 = new SimpleDateFormat("HHmmss");
DateFormat dateFormat5 = new SimpleDateFormat("hh:mm:ss a");
String s = Query.getString(1);
if (s.length()==5) s = "0"+s;
System.out.println(dateFormat5.format(dateFormat21.parse(s)));
But there's no real problem in your code apart calling getString more than necessary.
Try using some regex like this:
(\d?\d)(\d\d)(\d\d)
Match group 1 is the hours, match group 2 the minute, match group 3 the seconds.
For more about regex in Java see here:
http://www.regular-expressions.info/java.html

how to convert specified time in particular timezone? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I am living in India so my timezone is IST so when I want to fix meeting with the UK client then I just put my time in field like 10 A.M to 11 A.M. Now How to convert this in GMT0000 that is UK time zone?
Basically I have textfied to enter time then after inserted there is one dropdown box that is containing all available timezone then when I select UK timezone then how to convert this process using Java?
Thanks in advance.. tell me to solve my issue..
Note: I don't want to do this manually? cause of selection it will be converted? that is my task.. hope you understand my problem..
Regards..
India time is GMT + 5.30. Then 10.00 -5.30 can consider as GMT time.
In java you can do something like this
DateFormat df = new SimpleDateFormat("HH:mm");
Date dateIST=df.parse("10:00"); // india time
df.setTimeZone(TimeZone.getTimeZone("GMT"));
System.out.println(df.format(dateIST));// This is UK time
(JAVA answer) In textfield(s) you just need to enter your local time. Take an input from that textfield(s) and pass it in following code.
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
simpleDateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));//or whatever timezone u want.
String gmtStrDate = simpleDateFormat.format(Calendar.getInstance().getTime());
Or you may find this code suited to your need (your question is unclear):
SimpleDateFormat inputFormat = new SimpleDateFormat("HH:mm");
Date date = inputFormat.parse("time from textfield");//like 11:44
inputFormat.setTimeZone((TimeZone.getTimeZone("IST")));
SimpleDateFormat outputFormat = new SimpleDateFormat("HH:mm");
outputFormat.setTimeZone((TimeZone.getTimeZone("GMT")));
String outputText = outputFormat.format(date);
Now outputText is your desired time in UK format.
Note : Here i assumed you are entering time in textfield like 13:44. You can accordingly change that in formatters.

Categories

Resources