SimpleDateFormat

E

Eric Sosman

Anyone know how to use SimpleDateFormat to get a lowercase am and pm?

You could use String.toLowerCase() on the whole thing, at the
risk of turning "Fri" into "fri". (If you're super-worried, you could
use a regex to locate the AM/PM and apply toLowerCase() only to that
portion.) Or you could extend the class. Other than that, I can't
think of anything.
 
A

Arne Vajhøj

Anyone know how to use SimpleDateFormat to get a lowercase am and pm?

Code snippet:

import java.text.DateFormatSymbols;
import java.text.SimpleDateFormat;
import java.util.Date;

public class AMPM {
public static void main(String[] args) {
Date d1 = new Date();
Date d2 = new Date(d1.getTime() + 12*60*60*1000L);
SimpleDateFormat sdf = new SimpleDateFormat("hh:mm a");
System.out.println(sdf.format(d1));
System.out.println(sdf.format(d2));
DateFormatSymbols sym = sdf.getDateFormatSymbols();
sym.setAmPmStrings(new String[] { "am", "pm" });
sdf.setDateFormatSymbols(sym);
System.out.println(sdf.format(d1));
System.out.println(sdf.format(d2));
}
}

Arne
 
E

Eric Sosman

You could use String.toLowerCase() on the whole thing, at the
risk of turning "Fri" into "fri". (If you're super-worried, you could
use a regex to locate the AM/PM and apply toLowerCase() only to that
portion.) Or you could extend the class. Other than that, I can't
think of anything.

<!Dope slap!>

Never mind; see Arne Vajhøj's response.
 
R

Roedy Green

Anyone know how to use SimpleDateFormat to get a lowercase am and pm?

there is no built in code for that. Think instead how to convert your
date/time string AM/PM to am/pm

There various ways you can do it with String.charAt String.indexOf
String.toLowerCase
--
Roedy Green Canadian Mind Products
http://mindprod.com
The modern conservative is engaged in one of man's oldest exercises in moral philosophy; that is,
the search for a superior moral justification for selfishness.
~ John Kenneth Galbraith (born: 1908-10-15 died: 2006-04-29 at age: 97)
 
L

Lew

Roedy said:
bob wrote, quoted or indirectly quoted someone who said :

there is no built in code for that. Think instead how to convert your
date/time string AM/PM to am/pm

There various ways you can do it with String.charAt String.indexOf
String.toLowerCase

Or use Arne's suggestion, which has the virtue of being compact and employing the standard API without having to post-process the output of 'format()'.
 
A

Arne Vajhøj

there is no built in code for that. Think instead how to convert your
date/time string AM/PM to am/pm

There various ways you can do it with String.charAt String.indexOf
String.toLowerCase

A slightly modified version of an old saying: some people
can code in C in any language.

Arne
 
R

Roedy Green

Or use Arne's suggestion, which has the virtue of being compact and employing the standard API without having to post-process the output of 'format()'.

but arne does not exist in my universe. I had with him long ago and
plonked him.
--
Roedy Green Canadian Mind Products
http://mindprod.com
The modern conservative is engaged in one of man's oldest exercises in moral philosophy; that is,
the search for a superior moral justification for selfishness.
~ John Kenneth Galbraith (born: 1908-10-15 died: 2006-04-29 at age: 97)
 
A

Arne Vajhøj

but arne does not exist in my universe. I had with him long ago and
plonked him.

That is your right.

But I will consider it somewhat an ego trip that you want people to read
your outdated posts because you don't want to read all posts to the
thread.

Arne
 
M

markspace

but arne does not exist in my universe. I had with him long ago and
plonked him.


Then maybe you should un-plonk him, because he write good code and has
valuable insights.
 
L

Lew

Roedy said:
Lew wrote, quoted or indirectly quoted someone who said :
Or use Arne's suggestion, which has the virtue of being compact and employing the standard API without having to post-process the output of 'format()'.

but arne [sic] does not exist in my universe. I had [it] with him long ago and
plonked him.

That neither invalidates his suggestion nor excuses your ignorance of the API.

I, too, was ignorant of the SimpleDateFormat API, at least of the methods Arne mentioned, but I am in the habit of reading the Javadocs. The lesson for me, for the OP, and for you, too, is to check the Javadocs, in this casesince the question related to 'SimpleDateFormat', the Javadocs for 'SimpleDateFormat'.

The trouble with your answer is that it was suboptimal in the context of the much, much better answer that Arne had provided. This was a disservice to the OP. I recommend that you swallow your ego and unplonk Arne. After all, you are the one who's all about providing good information to those wishing to learn Java, aren't you?
 
R

Roedy Green

Bad move. But that's just my opinion.

I try to balance. I don't plonk Lew, even though I find him grating.
He is just too full of useful information to give up.

Arne is intelligent, but I felt the much smaller number of gems he put
out were not worth the more vicious thorn jabs.

Perhaps I should unplonk him for a while to see if he has mellowed or
my hide has toughened.

--
Roedy Green Canadian Mind Products
http://mindprod.com
The modern conservative is engaged in one of man's oldest exercises in moral philosophy; that is,
the search for a superior moral justification for selfishness.
~ John Kenneth Galbraith (born: 1908-10-15 died: 2006-04-29 at age: 97)
 
E

Eric Sosman

I try to balance. I don't plonk Lew, even though I find him grating.
He is just too full of useful information to give up.

Arne is intelligent, but I felt the much smaller number of gems he put
out were not worth the more vicious thorn jabs.

Perhaps I should unplonk him for a while to see if he has mellowed or
my hide has toughened.

Be sure to announce your decision to Usenet at large. Burning
the ballots to produce white smoke is traditional.
 
R

RedGrittyBrick

Be sure to announce your decision to Usenet at large. Burning
the ballots to produce white smoke is traditional.

From excommunicant to infallibility in a single step?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top