Regex for SimpleDateFormat string [closed] - java

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

Related

Conversion of milliseconds to a specific date format [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
How can I convert the current time in milliseconds, which is a Long, to a date in specific format?
The format that I need is yyyy-MM-dd HH:mm. This should be of type Date, not String.
You are confused. The type Date is a number of milliseconds since January 1 1970 midnight UTC. It has no inherent format. There is a default system format for a Date, but you cannot alter it. You will need to format your Date as a String if you need that particular String format.

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));

is there simple method to get hijri date as DateTime? [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 found answer to hijri date in Islamic calendar but the code is very complex.
The Question
is there simple function to get it as DateTime type ?
Try using the Joda-Time API IslamicChronology. It's not part of the Android API, download the JAR and add it to your build path to use it in Android projects.
See Joda-Time documentation for Islamic calendar system.
Example code taken from that documentation:
// setup date object for midday on May Day 2004 (ISO year 2004)
DateTime dtISO = new DateTime(2004, 5, 1, 12, 0, 0, 0);
// find out what the same instant is using the Islamic Chronology
DateTime dtIslamic = dtISO.withChronology(IslamicChronology.getInstance());

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 parse a string into date? [closed]

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.

Categories

Resources