String.format alternative on CodenameOne












1















I'm trying to port a Java-based library to CodenameOne in order to use it in a cross-platform project but it uses a lot of Java standard APIs that I cannot find in CodenameOne, first of all String.format.



I have read this Q&A and I understood there are a few utility libraries which implement what's missing in base classes.
Is there a library class which implements String.format?



As an example, I need to do something like String.format("%02d:%02d:%02d", hh, mm, ss);










share|improve this question

























  • Looks like there is no alternative way, check this link stackoverflow.com/questions/36681724/…

    – Centos
    Nov 20 '18 at 10:11











  • I was reading the very same page. I understand what Shai says about multi-platform support but formatting strings this way is something really mainstream that I don't think has dependencies on the architecture

    – afe
    Nov 20 '18 at 10:15
















1















I'm trying to port a Java-based library to CodenameOne in order to use it in a cross-platform project but it uses a lot of Java standard APIs that I cannot find in CodenameOne, first of all String.format.



I have read this Q&A and I understood there are a few utility libraries which implement what's missing in base classes.
Is there a library class which implements String.format?



As an example, I need to do something like String.format("%02d:%02d:%02d", hh, mm, ss);










share|improve this question

























  • Looks like there is no alternative way, check this link stackoverflow.com/questions/36681724/…

    – Centos
    Nov 20 '18 at 10:11











  • I was reading the very same page. I understand what Shai says about multi-platform support but formatting strings this way is something really mainstream that I don't think has dependencies on the architecture

    – afe
    Nov 20 '18 at 10:15














1












1








1


1






I'm trying to port a Java-based library to CodenameOne in order to use it in a cross-platform project but it uses a lot of Java standard APIs that I cannot find in CodenameOne, first of all String.format.



I have read this Q&A and I understood there are a few utility libraries which implement what's missing in base classes.
Is there a library class which implements String.format?



As an example, I need to do something like String.format("%02d:%02d:%02d", hh, mm, ss);










share|improve this question
















I'm trying to port a Java-based library to CodenameOne in order to use it in a cross-platform project but it uses a lot of Java standard APIs that I cannot find in CodenameOne, first of all String.format.



I have read this Q&A and I understood there are a few utility libraries which implement what's missing in base classes.
Is there a library class which implements String.format?



As an example, I need to do something like String.format("%02d:%02d:%02d", hh, mm, ss);







java cross-platform codenameone






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 20 '18 at 10:17







afe

















asked Nov 20 '18 at 9:47









afeafe

104315




104315













  • Looks like there is no alternative way, check this link stackoverflow.com/questions/36681724/…

    – Centos
    Nov 20 '18 at 10:11











  • I was reading the very same page. I understand what Shai says about multi-platform support but formatting strings this way is something really mainstream that I don't think has dependencies on the architecture

    – afe
    Nov 20 '18 at 10:15



















  • Looks like there is no alternative way, check this link stackoverflow.com/questions/36681724/…

    – Centos
    Nov 20 '18 at 10:11











  • I was reading the very same page. I understand what Shai says about multi-platform support but formatting strings this way is something really mainstream that I don't think has dependencies on the architecture

    – afe
    Nov 20 '18 at 10:15

















Looks like there is no alternative way, check this link stackoverflow.com/questions/36681724/…

– Centos
Nov 20 '18 at 10:11





Looks like there is no alternative way, check this link stackoverflow.com/questions/36681724/…

– Centos
Nov 20 '18 at 10:11













I was reading the very same page. I understand what Shai says about multi-platform support but formatting strings this way is something really mainstream that I don't think has dependencies on the architecture

– afe
Nov 20 '18 at 10:15





I was reading the very same page. I understand what Shai says about multi-platform support but formatting strings this way is something really mainstream that I don't think has dependencies on the architecture

– afe
Nov 20 '18 at 10:15












1 Answer
1






active

oldest

votes


















0














You can use com.codename1.l10n.SimpleDateFormat to format time although personally I just use utility Java code to format as it's simpler. With Date we get into the timezone complexities and that's a pain in the neck.



I just usually do:



public static String twoDigits(int v) {
return v < 10 ? "0" + v : "" + v;
}


Then:



String t = twoDigits(hh) + ":" + twoDigits(mm) + ":" + twoDigits(ss);


Notice that this code is more efficient than the Format code. The Format call needs to parse the formatting then generate the resulting string which is a costly step. Probably won't be noticeable for most cases though.



The main problem we have with String.format() is it's presence in String. Since String is a core part of the implementation a complex method like that will add weight to every application regardless of need. Also implementing a method like that with so many nuances would mean things would work differently on the simulator than on the device. So it's highly unlikely that we'll ever add that method.



In fact on JavaSE that method is really just a form of MessageFormat which is something we could add in the codename1 l10n package. Incompatibility wouldn't be a problem and neither would size/complexity. This is something you can implement yourself and even submit as a pull request if you so desire. You can base your implementation on the Apache licensed harmony project sources or you can build a clean room implementation (which I often found to be easier).






share|improve this answer


























  • Thanks for your answer Shai. That's not enough for me, since there are a looooooot of occurrences of this method in the library I need to port (the one I pointed out was just one of like a thousand examples). I just don't feel like replacing each of them with a different method tailored exactly for the occasion (this time is SimpleDateFormat, another time something else) while Java offered a single method for everything.

    – afe
    Nov 21 '18 at 8:03











  • Also, imagine I have something like String.format("2 digits something %2.2d", n), how can I achieve the %2.2.d thing without using String.format?

    – afe
    Nov 21 '18 at 8:06











  • There is a digit count format method in the L10NManager. See my revised answer covering the problems in String.format and discussing an alternative that's closer to what you are looking for

    – Shai Almog
    Nov 22 '18 at 4:11











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53390230%2fstring-format-alternative-on-codenameone%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














You can use com.codename1.l10n.SimpleDateFormat to format time although personally I just use utility Java code to format as it's simpler. With Date we get into the timezone complexities and that's a pain in the neck.



I just usually do:



public static String twoDigits(int v) {
return v < 10 ? "0" + v : "" + v;
}


Then:



String t = twoDigits(hh) + ":" + twoDigits(mm) + ":" + twoDigits(ss);


Notice that this code is more efficient than the Format code. The Format call needs to parse the formatting then generate the resulting string which is a costly step. Probably won't be noticeable for most cases though.



The main problem we have with String.format() is it's presence in String. Since String is a core part of the implementation a complex method like that will add weight to every application regardless of need. Also implementing a method like that with so many nuances would mean things would work differently on the simulator than on the device. So it's highly unlikely that we'll ever add that method.



In fact on JavaSE that method is really just a form of MessageFormat which is something we could add in the codename1 l10n package. Incompatibility wouldn't be a problem and neither would size/complexity. This is something you can implement yourself and even submit as a pull request if you so desire. You can base your implementation on the Apache licensed harmony project sources or you can build a clean room implementation (which I often found to be easier).






share|improve this answer


























  • Thanks for your answer Shai. That's not enough for me, since there are a looooooot of occurrences of this method in the library I need to port (the one I pointed out was just one of like a thousand examples). I just don't feel like replacing each of them with a different method tailored exactly for the occasion (this time is SimpleDateFormat, another time something else) while Java offered a single method for everything.

    – afe
    Nov 21 '18 at 8:03











  • Also, imagine I have something like String.format("2 digits something %2.2d", n), how can I achieve the %2.2.d thing without using String.format?

    – afe
    Nov 21 '18 at 8:06











  • There is a digit count format method in the L10NManager. See my revised answer covering the problems in String.format and discussing an alternative that's closer to what you are looking for

    – Shai Almog
    Nov 22 '18 at 4:11
















0














You can use com.codename1.l10n.SimpleDateFormat to format time although personally I just use utility Java code to format as it's simpler. With Date we get into the timezone complexities and that's a pain in the neck.



I just usually do:



public static String twoDigits(int v) {
return v < 10 ? "0" + v : "" + v;
}


Then:



String t = twoDigits(hh) + ":" + twoDigits(mm) + ":" + twoDigits(ss);


Notice that this code is more efficient than the Format code. The Format call needs to parse the formatting then generate the resulting string which is a costly step. Probably won't be noticeable for most cases though.



The main problem we have with String.format() is it's presence in String. Since String is a core part of the implementation a complex method like that will add weight to every application regardless of need. Also implementing a method like that with so many nuances would mean things would work differently on the simulator than on the device. So it's highly unlikely that we'll ever add that method.



In fact on JavaSE that method is really just a form of MessageFormat which is something we could add in the codename1 l10n package. Incompatibility wouldn't be a problem and neither would size/complexity. This is something you can implement yourself and even submit as a pull request if you so desire. You can base your implementation on the Apache licensed harmony project sources or you can build a clean room implementation (which I often found to be easier).






share|improve this answer


























  • Thanks for your answer Shai. That's not enough for me, since there are a looooooot of occurrences of this method in the library I need to port (the one I pointed out was just one of like a thousand examples). I just don't feel like replacing each of them with a different method tailored exactly for the occasion (this time is SimpleDateFormat, another time something else) while Java offered a single method for everything.

    – afe
    Nov 21 '18 at 8:03











  • Also, imagine I have something like String.format("2 digits something %2.2d", n), how can I achieve the %2.2.d thing without using String.format?

    – afe
    Nov 21 '18 at 8:06











  • There is a digit count format method in the L10NManager. See my revised answer covering the problems in String.format and discussing an alternative that's closer to what you are looking for

    – Shai Almog
    Nov 22 '18 at 4:11














0












0








0







You can use com.codename1.l10n.SimpleDateFormat to format time although personally I just use utility Java code to format as it's simpler. With Date we get into the timezone complexities and that's a pain in the neck.



I just usually do:



public static String twoDigits(int v) {
return v < 10 ? "0" + v : "" + v;
}


Then:



String t = twoDigits(hh) + ":" + twoDigits(mm) + ":" + twoDigits(ss);


Notice that this code is more efficient than the Format code. The Format call needs to parse the formatting then generate the resulting string which is a costly step. Probably won't be noticeable for most cases though.



The main problem we have with String.format() is it's presence in String. Since String is a core part of the implementation a complex method like that will add weight to every application regardless of need. Also implementing a method like that with so many nuances would mean things would work differently on the simulator than on the device. So it's highly unlikely that we'll ever add that method.



In fact on JavaSE that method is really just a form of MessageFormat which is something we could add in the codename1 l10n package. Incompatibility wouldn't be a problem and neither would size/complexity. This is something you can implement yourself and even submit as a pull request if you so desire. You can base your implementation on the Apache licensed harmony project sources or you can build a clean room implementation (which I often found to be easier).






share|improve this answer















You can use com.codename1.l10n.SimpleDateFormat to format time although personally I just use utility Java code to format as it's simpler. With Date we get into the timezone complexities and that's a pain in the neck.



I just usually do:



public static String twoDigits(int v) {
return v < 10 ? "0" + v : "" + v;
}


Then:



String t = twoDigits(hh) + ":" + twoDigits(mm) + ":" + twoDigits(ss);


Notice that this code is more efficient than the Format code. The Format call needs to parse the formatting then generate the resulting string which is a costly step. Probably won't be noticeable for most cases though.



The main problem we have with String.format() is it's presence in String. Since String is a core part of the implementation a complex method like that will add weight to every application regardless of need. Also implementing a method like that with so many nuances would mean things would work differently on the simulator than on the device. So it's highly unlikely that we'll ever add that method.



In fact on JavaSE that method is really just a form of MessageFormat which is something we could add in the codename1 l10n package. Incompatibility wouldn't be a problem and neither would size/complexity. This is something you can implement yourself and even submit as a pull request if you so desire. You can base your implementation on the Apache licensed harmony project sources or you can build a clean room implementation (which I often found to be easier).







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 22 '18 at 4:10

























answered Nov 21 '18 at 5:15









Shai AlmogShai Almog

40.9k52555




40.9k52555













  • Thanks for your answer Shai. That's not enough for me, since there are a looooooot of occurrences of this method in the library I need to port (the one I pointed out was just one of like a thousand examples). I just don't feel like replacing each of them with a different method tailored exactly for the occasion (this time is SimpleDateFormat, another time something else) while Java offered a single method for everything.

    – afe
    Nov 21 '18 at 8:03











  • Also, imagine I have something like String.format("2 digits something %2.2d", n), how can I achieve the %2.2.d thing without using String.format?

    – afe
    Nov 21 '18 at 8:06











  • There is a digit count format method in the L10NManager. See my revised answer covering the problems in String.format and discussing an alternative that's closer to what you are looking for

    – Shai Almog
    Nov 22 '18 at 4:11



















  • Thanks for your answer Shai. That's not enough for me, since there are a looooooot of occurrences of this method in the library I need to port (the one I pointed out was just one of like a thousand examples). I just don't feel like replacing each of them with a different method tailored exactly for the occasion (this time is SimpleDateFormat, another time something else) while Java offered a single method for everything.

    – afe
    Nov 21 '18 at 8:03











  • Also, imagine I have something like String.format("2 digits something %2.2d", n), how can I achieve the %2.2.d thing without using String.format?

    – afe
    Nov 21 '18 at 8:06











  • There is a digit count format method in the L10NManager. See my revised answer covering the problems in String.format and discussing an alternative that's closer to what you are looking for

    – Shai Almog
    Nov 22 '18 at 4:11

















Thanks for your answer Shai. That's not enough for me, since there are a looooooot of occurrences of this method in the library I need to port (the one I pointed out was just one of like a thousand examples). I just don't feel like replacing each of them with a different method tailored exactly for the occasion (this time is SimpleDateFormat, another time something else) while Java offered a single method for everything.

– afe
Nov 21 '18 at 8:03





Thanks for your answer Shai. That's not enough for me, since there are a looooooot of occurrences of this method in the library I need to port (the one I pointed out was just one of like a thousand examples). I just don't feel like replacing each of them with a different method tailored exactly for the occasion (this time is SimpleDateFormat, another time something else) while Java offered a single method for everything.

– afe
Nov 21 '18 at 8:03













Also, imagine I have something like String.format("2 digits something %2.2d", n), how can I achieve the %2.2.d thing without using String.format?

– afe
Nov 21 '18 at 8:06





Also, imagine I have something like String.format("2 digits something %2.2d", n), how can I achieve the %2.2.d thing without using String.format?

– afe
Nov 21 '18 at 8:06













There is a digit count format method in the L10NManager. See my revised answer covering the problems in String.format and discussing an alternative that's closer to what you are looking for

– Shai Almog
Nov 22 '18 at 4:11





There is a digit count format method in the L10NManager. See my revised answer covering the problems in String.format and discussing an alternative that's closer to what you are looking for

– Shai Almog
Nov 22 '18 at 4:11




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53390230%2fstring-format-alternative-on-codenameone%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







這個網誌中的熱門文章

Tangent Lines Diagram Along Smooth Curve

Yusuf al-Mu'taman ibn Hud

Zucchini