我正在尝试基于给定的语言环境以不同的方式在Java中格式化日期.例如,我希望英语用户看到"2009年11月1日"(由"MMM d,yyyy"格式化)和挪威用户看到"1. nov.2009"("d.MMM.yyyy").
如果我将语言环境添加到SimpleDateFormat构造函数中,那么月份部分可以正常工作,但其余部分呢?
我希望我可以将与locales配对的格式字符串添加到SimpleDateFormat,但我找不到任何方法来执行此操作.是否可以或者我是否需要让我的代码检查语言环境并添加相应的格式字符串?
根据文档,您可以在List中的任何位置插入对象:
该接口的用户可以精确控制列表中每个元素的插入位置.
(来源:http://download.oracle.com/javase/6/docs/api/java/util/List.html)
但是以下程序因IndexOutOfBoundsException而失败:
import java.util.ArrayList;
public class Test {
public static void main(String[] args) {
ArrayList<String> myList = new ArrayList<String>();
myList.add(0, "derp");
myList.add(2, "herp");
for (String s : myList) {
System.out.println("Le string: " + s);
}
}
}
Run Code Online (Sandbox Code Playgroud)
它也没有明确地设置初始容量(这是有道理的,因为默认值是10).
为什么我不能在任何位置插入对象,只要它的索引低于容量?尺寸是否始终等于插入元素的数量?
我在这里完全不解.我有一个对象列表,每个对象包含一个位置.我使用google.maps.geocoder查找此位置,然后我在地图上放置了该位置的标记.
但由于某种原因,只出现一个标记.我想这与我在这里的其他线程中看到的闭包问题有关,但我似乎无法将解决方案应用于我所拥有的.
我的代码如下:
var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
map.fitBounds(bounds);
for (var item in list) {
var geocoder = new google.maps.Geocoder();
var geoOptions = {
address: item.location,
bounds: bounds,
region: "NO"
};
geocoder.geocode(geoOptions, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
addMarker(map, item, results[0].geometry.location);
} else {
console.log("Geocode failed " + status);
}
});
}
function addMarker(map, item, location) {
var marker = new google.maps.Marker({ map : map, position : location});
marker.setTitle(item.title);
var infowindow = new google.maps.InfoWindow( {
content : …
Run Code Online (Sandbox Code Playgroud) 我正在尝试生成一个SVG图像,然后使用Apache Batik将其转码为PNG.但是,我最终得到一个空图像,我不明白为什么.
我使用SVGDomImplementation中的Document作为我的代码转换的基础(避免将SVG写入磁盘并再次加载).这是一个例子:
DOMImplementation domImpl = SVGDOMImplementation.getDOMImplementation();
String namespace = SVGDOMImplementation.SVG_NAMESPACE_URI;
Document document = domImpl.createDocument(namespace, "svg", null);
//stuff that builds SVG (and works)
TranscoderInput transcoderInput = new TranscoderInput(svgGenerator.getDOMFactory());
PNGTranscoder transcoder = new PNGTranscoder();
transcoder.addTranscodingHint(PNGTranscoder.KEY_WIDTH, new Float(svgWidth));
transcoder.addTranscodingHint(PNGTranscoder.KEY_HEIGHT, new Float(svgHeight));
try {
File temp = File.createTempFile(key, ".png");
FileOutputStream outputstream = new FileOutputStream(temp);
TranscoderOutput output = new TranscoderOutput(outputstream);
transcoder.transcode(transcoderInput, output);
outputstream.flush();
outputstream.close();
name = temp.getName();
} catch (IOException ioex) {
ioex.printStackTrace();
} catch (TranscoderException trex) {
trex.printStackTrace();
}
Run Code Online (Sandbox Code Playgroud)
我的问题是结果图像是空的,我不明白为什么.任何提示?
我正在尝试编写一些Junit测试来测试我们的应用程序中的旧类.代码正在尝试加载ResourceBundle(用于翻译)但失败了.我想问题是类路径相关但我无法弄明白.
代码在/ src中布局,我的测试在/ test中.在给定相对于/ src的基本名称的情况下加载ResourceBundle,例如"foo/bar/baz".
我的测试使用与app本身相同的类路径,所以我不明白它为什么找不到捆绑包.
对于什么是错的任何想法?
我突然体验到这种耙子和铁轨有点奇怪的输出.它最重要的看起来像一个进程列表,但显然Ruby/Rails相关.此外,它有几页长 - 我实际上必须增加终端中的回滚设置,以便在此输出开始之前查看发生了什么.
这是一个简短的摘录:
0.00 48.12 0.00 1 0.00 0.00 Rails::Rack::LogTailer#tail!
0.00 48.12 0.00 3 0.00 0.00 WEBrick::HTTPResponse#[]
0.00 48.12 0.00 1 0.00 0.00 Rack::Utils::HeaderHash#each
0.00 48.12 0.00 2 0.00 0.00 Range#begin
0.00 48.12 0.00 1 0.00 0.00 Range#end
0.00 48.12 0.00 1 0.00 10.00 Rack::File#each
0.00 48.12 0.00 1 0.00 0.00 WEBrick::HTTPRequest#fixup
0.00 48.12 0.00 1 0.00 0.00 Kernel.raise
0.00 48.12 0.00 1 0.00 0.00 Exception#to_s
0.00 48.12 0.00 1 0.00 0.00 WEBrick::GenericServer#stop
0.00 48.12 0.00 1 0.00 0.00 …
Run Code Online (Sandbox Code Playgroud) 我正在为C/C++中的node.js开发一个简单的模块.该模块依赖于第三方(开源)C库,可以在我的模块可以编译的机器上安装或不安装.
我的问题是,我该如何处理这种依赖?bindings.gyp是否提供了我可以使用的某种机制,我只是捆绑它(如果那么,怎么样?),或者我应该记录下来并告知用户在编译我的模块之前安装库?
我使用 PostgreSQL 9.5 和 JSONB 数据类型来存储文档。我的桌子是这样的:
create table records (
id serial,
data jsonb
);
Run Code Online (Sandbox Code Playgroud)
我的文档包含一系列对象,例如:
{
"some_field": "a value",
"another_field": 123,
entries: [
{
"name": "John Doe",
"age": 42
},
{
"name": "Johnny McMuffin",
"age": 117
}
]
}
Run Code Online (Sandbox Code Playgroud)
问题是我希望能够过滤数组name
中的属性entries
,但我无法弄清楚。我希望能够在表中找到与对象列表中的名称之一部分匹配的行。
我已经阅读了很多有关索引和表达式之类的内容,但我似乎无法让它发挥作用。这不应该是可能的吗?
我在main
包中声明我的数据库连接,有点像这样:
package main
var Db *sql.DB
func main() {
var err error
db, err = sql.Open("postgres", "stuff...")
// error handling and more app stuff
}
Run Code Online (Sandbox Code Playgroud)
首先,我将所有代码都放在包中main
,并且可以从其他文件访问数据库连接。但是,我想将代码分成包,现在变量Db
不再可见。
是否可以跨包共享它,或者我是否必须将其作为参数添加到我的main
函数中对其他包的任何调用中?
在我的测试中,我有一个可以从以下结构中获取值的函数:
func getField(v interface{}, field string) string {
r := reflect.ValueOf(v)
f := reflect.Indirect(r).FieldByName(field)
t := f.Kind()
switch t {
case reflect.Int, reflect.Int64:
return strconv.FormatInt(f.Int(), 10)
case reflect.String:
return f.String()
case reflect.Bool:
if f.Bool() {
return "true"
}
return "false"
return ""
}
Run Code Online (Sandbox Code Playgroud)
这适用于上面的类型,但我无法使用它time.Time
。我该怎么做呢?
java ×4
go ×2
arrays ×1
batik ×1
database ×1
date ×1
google-maps ×1
javascript ×1
jsonb ×1
junit ×1
list ×1
locale ×1
node.js ×1
png ×1
postgresql ×1
rake ×1
reflection ×1
ruby ×1
svg ×1
transcode ×1
unit-testing ×1